Remove substring list from String - Python
Last Updated :
19 May, 2025
Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by removing all specified substrings efficiently.
Using String Replace in a Loop
This method iterates through the list of substrings and removes each from the string using the replace method.
Python
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
for sub in a:
s = s.replace(sub, "")
print(s)
Explanation:
- The replace() method is used to remove each substring by replacing it with an empty string.
- The loop ensures all substrings in sublist 'a' are processed.
Using Regular Expressions
Regular expressions provide a powerful way to remove multiple substrings in one step.
Python
import re
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
pattern = "|".join(map(re.escape, a))
s = re.sub(pattern, "", s)
print(s)
Explanation:
- re.escape() ensures special characters in substrings are handled safely.
- join() method creates a single pattern matching all substrings.
- re.sub() method replaces all occurrences of the pattern with an empty string.
Using List Comprehension and Join
This method builds a filtered string by excluding unwanted substrings.
Python
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = " ".join(word for word in s.split() if all(sub not in word for sub in a))
print(res)
Explanation: This code removes words containing any substring from list "a" in the string "str". It splits the string into words, then keeps only those words that do not contain any substring from "a", and finally joins them back into a string.
reduce() function allows for successive application of the replace method.
Python
from functools import reduce
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = reduce(lambda acc, sub: acc.replace(sub, ""), a, s)
print(res)
Explanation: This code removes all substrings in list "a" from the string s using reduce. It repeatedly applies str.replace() for each substring in "a", replacing them with an empty string. The final result is the original string with all occurrences of "Geeks" and "awesome" removed.
Using a Custom Function with Loop
A custom function provides flexibility to handle edge cases or additional processing.
Python
def remove_substrings(s, a):
for sub in a:
s = s.replace(sub, "")
return s
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = remove_substrings(s, a)
print(res)
Explanation: remove_substrings iterates through each substring in list a and removes all its occurrences from the given string s using replace(). It returns the cleaned string after all specified substrings have been removed. In this example, it removes "Geeks" and "awesome" from the original string.
Related Articles:
Similar Reads
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Remove String from String List This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certa
4 min read
Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which
5 min read
Python - Remove after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing
3 min read
Remove URLs from string in Python A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I
3 min read