Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Replace Substrings from String List - Python
Next article icon

Python – Remove substring list from String

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python Strings we encounter problems where we need to remove a substring from a string. However, in some cases, we need to handle a list of substrings to be removed, ensuring the string is adjusted accordingly.

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."  #sub list a = ["Geeks", "awesome"]  # Remove substrings using replace for sub in a:     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.

Let’s explore some more methods and see how we can remove substring list from string.

Table of Content

  • Using Regular Expressions
  • Using List Comprehension and Join
  • Using Reduce from functools
  • Using a Custom Function with Loop

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." #sublist a = ["Geeks", "awesome"]  # Create a regex pattern from sublist a 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."  #sublist a = ["Geeks", "awesome"]  # Remove substrings using join and list comprehension result = " ".join(word for word in s.split() if all(sub not in word for sub in a))  print(result) 

Output
is an website. 

Explanation:

  • The string is split into words and each word is checked against the sublist ‘a’.
  • Only words that do not contain any substrings are included in the result.
  • This method works well for word-level substring removal but might miss partial matches.

Using Reduce from functools

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

Python
from functools import reduce  s = "GeeksforGeeks is an awesome website." #sublist a = ["Geeks", "awesome"]  # Use reduce to remove substrings res = reduce(lambda acc, sub: acc.replace(sub, ""), a, s)  print(res) 

Output
for is an  website. 

Explanation:

  • The reduce() function applies the replace() method iteratively over the substrings.
  • This approach condenses the logic into a single functional expression.

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." #sublist a = ["Geeks", "awesome"] res = remove_substrings(s, a)  print(res) 

Output
for is an  website. 

Explanation:

  • This approach encapsulates the logic in a reusable function.
  • The function can be extended to handle additional operations if needed.


Next Article
Replace Substrings from String List - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • 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
  • Replace Substrings from String List - Python
    The task of replacing substrings in a list of strings involves iterating through each string and substituting specific words with their corresponding replacements. For example, given a list a = ['GeeksforGeeks', 'And', 'Computer Science'] and replacements b = [['Geeks', 'Gks'], ['And', '&'], ['C
    3 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 | 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 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 comprehensionUsin
    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
  • Python | Remove the given substring from end of string
    Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Remove the substring from the end of the string using Slicing In thi
    3 min read
  • Replace substring in list of strings - Python
    We are given a list of strings, and our task is to replace a specific substring within each string with a new substring. This is useful when modifying text data in bulk. For example, given a = ["hello world", "world of code", "worldwide"], replacing "world" with "universe" should result in ["hello u
    3 min read
  • Python | Remove prefix strings from list
    Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati
    5 min read
  • Python - Remove leading 0 from Strings List
    Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
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