Data type is essential in all languages because it allows us to enter any type of data as an input. In python, we use a string to store the sequence of the alphabet or a whole sentence. In the string, multiple sequences of characters are referred to as a substring. For example, if we have the string “QOPTWE” then the “PTW” is considered a substring. While dealing with strings, you may need to remove the substring from a string to serve the specific programming purpose. In Python, we have numerous ways that help us to remove a substring from a string.
In this post, we demonstrate three methods to remove or replace a substring from a string in Python:
- Using replace() Method in Python
- Using Loop Cycle With replace() Method in Python
- Using Index Method in Python
Now let’s discuss these methods in detail to make you able to remove a substring from a string in python.
Method 1: Using replace() Function to Remove or Replace Substring from String
The first method we use is the string replace() function. Using this function, we can remove or replace any substring from a string in python. The syntax of the replace() function is provided below:
- “old_value” is the value which we want to replace or remove.
- “new_value” is the value that we input to take the position of the old value. In case we do not input a new value, then the old value will be removed without being replaced.
- “count_occurrence” will allow us to remove the old value according to our own will. It means that if we have more than three same values in our string and we want to remove only two, then we pass the value of “two” in the count parameter.
When count value is “0” or nothing is passed:
#when count value become default string_value = "one thousand one hundred and twenty one." substring = string_value.replace("one", "three") print(substring)
In the above code:
- We are creating a string named “string_value”.
- Secondly, we are using the string replace function to remove the old value given as “one” with the new value “three”.
- Here, we are not passing any count value, so all the old values will be replaced by the new value.
The above code output shows that the string value “One” is replaced by “Three”.
When count value is set to “2”:
#when count value becomes 2 string_value = "one thousand one hundred and twenty-one." substring = string_value.replace("one", "three", 2) print(substring)
In the above code, we have passed the count value “2”. Which states only two occurrences of the old values will be changed by the new value. The last value will remain the same. By using this, we can change the value according to our requirements.
In the output, we can see that the last value name “one” is not changing to a new value named ”three”. This is because we only want to change the value for the first two substrings.
Method 2: Using Loop Cycle with replace() Method
In this method, we used the Loop cycle with replace() function. The for loop is used alongside replace() method to create a loop cycle. This method makes us able to remove multiple substrings from a string.
string_value = "two thousand and twenty two" print("The Original string value : " + string_value) sub_string = ["thousand","and"] for sub in sub_string: string_value= string_value.replace(' ' + sub + ' ', ' ') print("After Removing : " + string_value)
In the above code:
- First, we initialized the string named as “string_value” and printed the original value of the string.
- Secondly, we initialize a substring value “thousand” and ”and” that we want to remove from the string.
- After that, we started the for loop cycle and defined the replace function in his main body. The value will be removed because we do not input any new value in the replace parameter.
- The new string value will be printed without the substring value.
The above output shows that the substring value named “thousand” and “and” will be removed from the main string value.
Method 3: Using the Index Method to Remove Substrings
Index Method helps us to find the position of any string, element of character, or list of elements. The syntax of the index string method is shown below:
String has a “start” value and an “end” value. If we want to jump from one index number to another, it also supports the step value. Any value defined using the string data type has its own index number starting from “n” to “n+1”, “n+2”, and so on.
In this method, we are removing substring from the string using its index value. Every character in the string has its own index value, so we will remove any type of substring from a string.
string_value = 'One Thousand One Hundred And Twenty One' print("The Original String Value is: ",string_value) new_string = string_value[:4] + string_value[13:] print("After Removing Substring: ",new_string)
In the above code:
- First, we have initialized a string named “string value” and printed the original string on screen.
- Secondly, we use the index method to remove substring values from a string. The removing substring value starts from index number “4” and ends at index number “13”. By playing with index numbers, we can remove any substring.
- In this string, we have removed the substring “thousand” from the original string.
The output of the code shows us that we removed the thousand from the original string, and using this same method, we can remove any substring.
In python, you can remove substrings from strings using the built-in “replace()”, “loop with replace() method”, and by changing the“index number” of the string. In the “replace()” function, we can change any substring using a simple command, considered an effective method. In the “loop with replace()” method, we can remove multiple substrings from a single string. While the “index number” method removes the substring using the index value.
In this post, all these methods are explained with suitable examples to remove a substring from a string.
The Best Linux Tutorials Website