While handling text data in python, we sometimes need to remove a specific substring from the text. In this article, we will discuss different ways to remove a substring from a string in Python.
- Remove Substring From String in Python Using split() Method
- Remove Substring From String in Python Using Using the join() Method
- Remove Substring From String in Python Using the replace() Method
- Remove Substring From String in PythonUsing Regular Expressions
- Remove Substring From String in Python by Index
- Conclusion
Remove Substring From String in Python Using split() Method
The split()
method in Python is used to split a string into substrings at a separator. The split()
method, when invoked on a string, takes a string in the form of a separator as its input argument. After execution, it returns a list of substrings from the original string, which is split at the separator.
To remove a substring from a string in Python using the split()
method, we will use the following steps.
- First, we will create an empty string named
output_string
to store the output string. - Then, we will use the
split()
method to split the string into substrings from the positions where we need to remove a specific substring. For this, we will invoke thesplit()
method on the input string with the substring that needs to be removed as the input argument. After execution, thesplit()
method will return a string of substrings. We will assign the list to a variablestr_list
. - Once we get the list of strings, we will iterate through the substrings in
str_list
using a for loop. During iteration, we will add the current substring tooutput_string
using the string concatenation operation.
After execution of the for loop, we will get the required output string in the variable output_string
. You can observe this in the following code.
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
output_string = ""
str_list = myStr.split(substring)
for element in str_list:
output_string += element
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
In the output, you can observe that the substring python
has been removed from the input string.
Remove Substring From String in Python Using Using the join() Method
Performing string concatenation several times requires unnecessary storage and time. Therefore, we can avoid that by using the join()
method.
The join()
method, when invoked on a separator string, takes an iterable object as its input argument. After execution, it returns a string consisting of the elements of the iterable object separated by the separator string.
To remove substring from a string in python using the join()
method, we will use the following steps.
- First, we will use the
split()
method to split the input string into substrings from the positions where we need to remove a specific substring. For this, we will invoke thesplit()
method on the input string with the substring that needs to be removed as the input argument. After execution, thesplit()
method will return a string of substrings. We will assign the list to a variablestr_list
. - Next, we will invoke the
join()
method on an empty string withstr_list
as its input argument.
After execution of the join()
method, we will get the required string output as shown below.
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
str_list = myStr.split(substring)
output_string = "".join(str_list)
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
Here, you can observe that we have converted the list returned by the split()
method into a string using the join()
method. Thus, we have avoided repeated string concatenation as we did in the previous example.
Remove Substring From String in Python Using the replace() Method
The replace()
method is used to replace one or more characters from a string in python. When invoked on a string, the replace()
method takes two substrings as its input argument. After execution, it replaces the substring in the first argument with that of the second input argument. Then it returns the modified string.
To remove a substring from a string using the replace()
method, we will invoke the replace()
method on the original string with the substring that is to be removed as the first input argument and an empty string as the second input argument.
After execution of the replace()
method, we will get the output string as shown in the following example.
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
output_string = myStr.replace(substring, "")
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
Here, we have removed the required substring from the input string in a single statement using the replace()
method.
Remove Substring From String in PythonUsing Regular Expressions
Regular expressions provide us with efficient ways to manipulate strings in Python. We can also use regular expressions to remove a substring from a string in python. For this, we can use the re.split()
method and the re.sub()
method.
Remove Substring From String in Python Using re.split() Method
The re.split()
method is used to split a text at a specified separator. The re.split()
method takes a separator string as its first input argument and the text string as its second input argument. After execution, it returns a list of strings from the original string that are separated by the separator.
To remove a substring from a string in Python using the re.split()
method, we will use the following steps.
- First, we will create an empty string named
output_string
to store the output string. - Then, we will use the
re.split()
method to split the string into substrings from the positions where we need to remove a specific substring. For this, we will execute there.split()
method with the substring that needs to be removed as its first input argument and the text string as its second input argument. After execution, there.split()
method will return a string of substrings. We will assign the list to a variablestr_list
. - Once we get the list of strings, we will iterate through the substrings in
str_list
using a for loop. During iteration, we will add the current substring tooutput_string
using the string concatenation operation.
After execution of the for loop, we will get the required output string in the variable output_string
. You can observe this in the following code.
import re
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
output_string = ""
str_list = re.split(substring, myStr)
for element in str_list:
output_string += element
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
You can observe that the approach using the re.split()
method is almost similar to the approach using the string split()
method. However, both approaches have different execution speeds. If the input string is very large, the re.split()
method should be the preferred choice to split the input string.
Performing string concatenation several times requires unnecessary memory and time. Therefore, we can avoid that by using the join()
method.
To remove substring from a string in python using the join()
method, we will use the following steps.
- First, we will use the
re.split()
method to split the input string into substrings from the positions where we need to remove a specific substring.For this, we will execute there.split()
method with the substring that has to be removed as its first input argument and the text string as its second input argument. After execution, there.split()
method will return a string of substrings. We will assign the list to a variablestr_list
.
- Next, we will invoke the
join()
method on an empty string withstr_list
as its input argument.
After execution of the join()
method, we will get the required string output as shown below.
import re
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
str_list = re.split(substring, myStr)
output_string = "".join(str_list)
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
In this approach, we have obtained the output string in only two python statements. Also, we haven’t done repetitive string concatenation which takes unnecessary time.
Remove Substring From String in Python Using re.sub() Method
The re.sub()
method is used to substitute one or more characters from a string in python. The re.sub()
method takes three input arguments. The first input argument is the substring that needs to be substituted. The second input argument is the substitute substring. The original string is passed as the third input string.
After execution, the re.sub()
method replaces the substring in the first argument with that of the second input argument. Then it returns the modified string.
To remove a substring from a string using the re.sub()
method, we will execute the re.sub()
method with the substring that is to be removed as the first input argument, an empty string as the second input argument, and the original string as the third input argument.
After execution of the re.sub()
method, we will get the output string as shown in the following example.
import re
myStr = "I am PFB. I provide free python tutorials for you to learn python."
substring = "python"
output_string = re.sub(substring, "", myStr)
print("The input string is:", myStr)
print("The substring is:", substring)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The substring is: python
The output string is: I am PFB. I provide free tutorials for you to learn .
The re.sub()
method works in a similar manner to the replace()
method. However, it is faster than the latter and should be the preferred choice.
Remove Substring From String in Python by Index
Sometimes, we might need to remove a substring from a string when we know its position in the string. To remove a substring from a string in python by index, we will use string slicing.
If we have to remove the substring from index i to j, we will make two slices of the string. The first slice will be from index 0 to i-1 and the second slice will be from index j+1 to the last character.
After obtaining the slices, we will concatenate the slices to obtain the output string as shown in the following example.
import re
myStr = "I am PFB. I provide free python tutorials for you to learn python."
output_string = myStr[0:5]+myStr[11:]
print("The input string is:", myStr)
print("The output string is:", output_string)
Output:
The input string is: I am PFB. I provide free python tutorials for you to learn python.
The output string is: I am provide free python tutorials for you to learn python.
Conclusion
In this article, we have discussed different ways to remove a substring from a string in Python. Out of all the approaches, the approaches using re.sub()
method and the replace()
method have the best time complexity. Therefore, I would suggest you use these approaches in your program.
I hope you enjoyed reading this article. To learn more about python programming, you can read this article on how to remove all occurrences of a character in a list in Python. You might also like this article on how to check if a python string contains a number.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.