Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove Substring list from String using Python
Removing multiple substrings from a string is a common task in Python text processing. Python provides several approaches including replace(), regular expressions, list comprehension, and the translate() method.
Using replace() Method
The simplest approach is to iterate through substrings and use replace() to remove each one ?
def remove_substrings_replace(main_string, substrings_to_remove):
for substring in substrings_to_remove:
main_string = main_string.replace(substring, "")
return main_string
text = "Hello, everyone! This is a extra string just for example."
unwanted = ["everyone", "extra", "just"]
result = remove_substrings_replace(text, unwanted)
print(result)
Hello, ! This is a string for example.
Using re Module with Pattern Matching
Regular expressions provide more control and efficiency when removing multiple substrings ?
import re
def remove_substrings_regex(main_string, substrings_to_remove):
pattern = "|".join(map(re.escape, substrings_to_remove))
return re.sub(pattern, "", main_string)
text = "Hello, everyone! This is a extra string just for example."
unwanted = ["everyone", "extra", "just"]
result = remove_substrings_regex(text, unwanted)
print(result)
Hello, ! This is a string for example.
Using List Comprehension with Word Filtering
This approach splits text into words and filters out any word containing unwanted substrings ?
def remove_substrings_comprehension(main_string, substrings_to_remove):
words = main_string.split()
filtered_words = [word for word in words if not any(sub in word for sub in substrings_to_remove)]
return ' '.join(filtered_words)
text = "Hello, everyone! This is a extra string just for example."
unwanted = ["everyone", "extra", "just"]
result = remove_substrings_comprehension(text, unwanted)
print(result)
Hello, ! This is a string for example.
Using translate() Method
The translate() method removes individual characters specified in a translation table ?
def remove_chars_translate(main_string, chars_to_remove):
translation_table = str.maketrans("", "", "".join(chars_to_remove))
return main_string.translate(translation_table)
text = "Hello, world! This is a sample string."
unwanted_chars = ["world", "sample"]
result = remove_chars_translate(text, unwanted_chars)
print(result)
H, ! Thi i ting.
Performance Comparison
| Method | Best For | Performance | Precision |
|---|---|---|---|
replace() |
Simple substring removal | Good | Exact matches |
re.sub() |
Complex patterns | Excellent | Pattern-based |
| List comprehension | Word-level filtering | Good | Word boundaries |
translate() |
Character removal | Excellent | Character-level |
Conclusion
Use re.sub() for efficient removal of multiple substrings. Choose replace() for simple cases and list comprehension when you need word-boundary awareness. The translate() method works best for removing individual characters.
