| by Arround The Web | No comments

Remove Substring from String in Python

The “string” data type is utilized for saving the sequence of the alphabet as well as the entire sentence. In Python, the string is immutable which means that the provided string cannot be changed. If developers want to modify the string, multiple methods/functions are available in Python that will return a new string without modifying the original string. These methods/functions also enable adding, changing, and removing the characters or a particular part of the string.

In this post, we will discuss the multiple methods to remove substrings from the string in Python.

How to Remove/Modify Particular Part of the String in Python?

To remove or replace a substring from the provided string in Python, the below-stated methods are used:

Method 1: How to Remove Substring From String Using the “replace()” Method in Python?

The “replace()” method is used to remove/replace the substring with the specified new string. However, the original string will not be changed. To do so, first, declare a string variable and pass it a string. Then, call the “print()” function to display the provided string:

my_str = "Welcome to LinuxHint World!"

print("Original String : " + my_str)

 
Now, invoke the “replace()” method inside the “print()” statement and pass it to the substring which needs to replace a new string/value as a replacement:

print("Resultant String : ", my_str.replace('Welcome', ''))

 
According to the below-given output, the specified substring has been removed successfully:

Method 2: How to Remove Substring From String Using the “for” Loop With “replace()” Method in Python?

The loop cycle along with the “replace()” method is another easiest way to remove the substring from the input string. By using this method, we can remove multiple substrings from the input string.

Let’s check out the provided code to perform this corresponding operation!

Example

The input string has been specified in the previous example. here, we have declared and initialized the substring variable with the values that need to remove from the string:

sub_str = ["Welcome", "to"]

 
Now, start the “for” loop cycle and define the “replace()” method in the loop’s main body. Here, we did not specify the new value in the replacement parameter. Therefore, the specified substring will be removed from the input string. Lastly, apply the print statement to get the resultant string:

for i in sub_str:
  my_str = my_str.replace('' + i + '', '')
print("Resultant String : ", my_str)

 
Output

Method 3: How to Remove Substring From String Using the “translate()” Method in Python?

The “translate()” method is also used for removing/replacing the single character or substring from the string by utilizing the translate table in Python. It takes a Unicode of a specified character that users want to be changed in an input string and a “None” identifier as a replacement to eliminate it from the specified string.

Now, check out the provided example for a better understanding!

Example

Call the “ord()” function inside the “translate()” method to get the Unicode of a specified character that is provided to the translate table and pass it to the string variable. Then, execute the “print()” function and pass it the string variable to get the resultant string:

r_str = my_str.translate({ord("o"): None})
print("Resultant String : ", r_str)

 
It can be seen that the specified character has been removed successfully from the input string:

Method 4: How to Remove Substring From String Using Index Method in Python?

Developers can also eliminate the substring by using the indexing method. It helps users to find the position of any string or list of elements. Users need to provide the starting and ending index of the desired substring to remove it from the string:

For practical demonstration, move to the provided example!

Example

By using the index method, we have specified the “0” as a substring starting index number and “11” as an ending index number. Then, call the “print()” method to display the resultant string:

r_str = my_str[:0] + my_str[11:]
print("Resultant String : ", r_str)

 
Output


That’s all! We have compiled the different ways to remove substrings from the string in Python.

Conclusion

In Python, multiple methods are used to remove the substring from a string, such as the “replace()” method, “for” loop with “replace()” method, “translate()” method, and the “Index” method. In this post. We have described the different methods for removing a particular part of the string in Python.

Share Button

Source: linuxhint.com

Leave a Reply