Skip to content
Related Articles

Related Articles

Python | Remove substring list from String

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes, while working with Python Strings, we can have problem in which we need to remove a substring from String. This is quite easy and many times solved before. But sometimes, we deal with list of strings that need to be removed and String adjusted accordingly. Lets discuss certain ways in which this task is achieved. 

Method #1: Using loop + replace() The combination of the above functions can be used to solve this problem. In this, we perform the replace of multiple Strings as they occur using replace(). 

Python3




# Python3 code to demonstrate working of
# Remove substring list from String
# Using loop + replace()
 
# initializing string
test_str = "gfg is best for all geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing sub list
sub_list = ["best", "all"]
 
# Remove substring list from String
# Using loop + replace()
for sub in sub_list:
    test_str = test_str.replace(' ' + sub + ' ', ' ')
 
# printing result
print("The string after substring removal : " + test_str)

Output : 

The original string is : gfg is best for all geeks
The string after substring removal : gfg is for geeks

Method #2 : Using replace() + join() + split() The combination of above functions can be used to solve this problem. In this, we perform the task of handling single space using join() + split(). 

Python3




# Python3 code to demonstrate working of
# Remove substring list from String
# Using replace() + join() + split()
 
# initializing string
test_str = "gfg is best for all geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing sub list
sub_list = ["best", "all"]
 
# Remove substring list from String
# Using replace() + join() + split()
for sub in sub_list:
    test_str = test_str.replace(sub, ' ')
res = " ".join(test_str.split())
 
# printing result
print("The string after substring removal : " + res)

Output : 

The original string is : gfg is best for all geeks
The string after substring removal : gfg is for geeks

Method #3: Using split() method, in and not in operators

Python3




# Python3 code to demonstrate working of
# Remove substring list from String
 
# initializing string
test_str = "gfg is best for all geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing sub list
sub_list = ["best", "all"]
b = test_str.split()
x = []
for i in b:
    if i not in sub_list:
        x.append(i)
res = " ".join(x)
 
# printing result
print("The string after substring removal : " + res)

Output

The original string is : gfg is best for all geeks
The string after substring removal : gfg is for geeks

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!