| by Arround The Web | No comments

Remove Substring from String in Python

Python offers an extensive range of methods for manipulating strings efficiently. String manipulation includes adding, removing, replacing, concatenating, etc. A “Substring” can be removed from a string in multiple scenarios, such as cleaning up data or modifying text inputs. This article examines various Python methods for removing substrings.

How to Remove/Eliminate Substring From Python String?

Python provides various methods for removing substrings from defined strings. These are some of the methods:

Method 1: Remove/Eliminate Substring From String Utilizing the “replace()” Method

The “replace()” method is utilized to replace occurrences of a substring with another particular string. Substrings can be deleted simply by replacing them with empty strings.

Syntax

string.replace(old, new, count)

 
This method returns a new string that replaces all instances of the original substring with the new substring. Only the first count occurrences are replaced if the optional argument count is given.

Example

Substrings are eliminated from strings via the below code:

str1 = "Welcome to Python Guide"
print("Given String: ",str1)
substr1 = "Guide"
print('\nAfter Removing Substring: ',str1.replace(substr1, ""))

 
In the above code, the “string.replace()” method takes the specified substring and empty string as its arguments, respectively to remove the substring from the initialized string.

Output


The substring has been eliminated from the defined string appropriately.

Method 2: Remove Substring From String Using the “re.sub()” Method

The “re.sub()” method of the “re” module is utilized to substitute patterns in strings using regular expressions. This method removes substrings by matching and replacing them with a null/empty string.

Syntax

re.sub(pattern, repl, string, count=0, flags=0)

 
From the above syntax:

    • pattern” specifies how the string should be matched with the regular expression.
    • repl” is the string to replace the matched pattern.
    • string” is the input string where the replacement has to be done.
    • count” is an optional argument specifying how many replacements are to be made and when this value is set to “0” all matches would be replaced.
    • flags” is an optional argument that modifies the pattern-matching process. For example, “re.IGNORECASE” makes the matching case insensitive.

Example

In the below code, the “re.sub()” method is utilized to eliminate/remove the substring:

import re
str1 = "Welcome to Python Guide"
print("Given String: ",str1)
substr1 = "Guide"
print('\nAfter Removing Substring: ',re.sub(substr1, "", str1))

 
According to the above code, the “re.sub()” method of the “re” module takes the substring, empty string, and the initialized string as its arguments, respectively to remove the specified substring from the given string.

Output


As analyzed, the specified substring has been eliminated from the string.

Method 3: Remove Substring From String Using the “split()” and “join()” Methods

The Python “split()” method breaks a string into a substring list according to a delimiter. To remove a substring from a string, this method splits the string based on the delimiter and then joins the resulting list using the “join()” method.

Syntax

string.split(separator, maxsplit)

 
Here the “separator” is the character or string to use as a delimiter, and “maxsplit” is the maximum number of splits to perform.

Example

Here is an example code:

str1 = "Welcome to Python Guide"
print("Given String: ",str1)
substr1 = "Guide"
print('\nAfter Removing Substring: ',"".join(str1.split(substr1)))

 
In the above code, the “join()” method is used along with the “split()” method to split the specified substring from the string and then join the resultant string.

Output


The defined string has been updated by removing the substring.

Method 4: Remove Substring From String Using the “String Slicing” Method

In Python, the string slicing method is used to slice the string based on the fetched index and length of the substring.

Example

Let’s use the below code to eliminate the substring from the given string:

str1 = "Welcome to Python Guide"
print("Given String: ",str1)
substr1 = "Guide"
index = str1.index(substr1)
new_string = str1[:index] + str1[index + len(substr1):]
print('\nAfter Removing Substring: ',new_string)

 
Based on the above code:

    • The string and the substring that needs to be deleted are initialized, respectively.
    • The “string.index()” method takes the substring as an argument and retrieves the index/position.
    • The “String Slicing” method utilizes the index value of the substring and removes the specified substring from the given string based on that.

Output

Method 5: Remove Substring From String Using the “strip()” Method

The “strip()” method removes leading and trailing characters from a string. The substrings can be removed from the string by passing them as “strip()” method arguments.

Syntax

string.strip(characters)

 
The “characters” attribute is not mandatory and indicates the set of characters to be deleted. If it is not given, all leading and trailing whitespaces are eliminated by default.

Example

The below code utilizes the “strip()” method to remove/delete the substring from the particular string:

str1 = "Welcome to Python Guide"
print("Given String: ",str1)
substr1 = "Guide"
print('\nAfter Removing Substring: ',str1.strip(substr1))

 
In the above code snippet, the “string.strip()” method takes the substring as an argument and removes it from the string.

Output

Conclusion

The “replace()”, “re.sub()”, “split()” with “join()”, “String Slicing”, and “strip()” methods are used to remove the substring from the string in Python. These approaches either remove the substring based on a delimiter, specified index, or string slicing. A detailed overview of removing substring from a string has been presented in this article using multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply