Movatterモバイル変換


[0]ホーム

URL:


Open In App

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"]forsubina:s=s.replace(sub,"")print(s)

Output
for is an  website.

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
importres="GeeksforGeeks is an awesome website."a=["Geeks","awesome"]pattern="|".join(map(re.escape,a))s=re.sub(pattern,"",s)print(s)

Output
for is an  website.

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(wordforwordins.split()ifall(subnotinwordforsubina))print(res)

Output
is an website.

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.

Using Reduce from functools

reduce() function allows for successive application of the replace method.

Python
fromfunctoolsimportreduces="GeeksforGeeks is an awesome website."a=["Geeks","awesome"]res=reduce(lambdaacc,sub:acc.replace(sub,""),a,s)print(res)

Output
for is an  website.

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
defremove_substrings(s,a):forsubina:s=s.replace(sub,"")returnss="GeeksforGeeks is an awesome website."a=["Geeks","awesome"]res=remove_substrings(s,a)print(res)

Output
for is an  website.

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:


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp