Open In App

Python | Remove substring list from String

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Example

Input: 'Hello world'
Output: 'World'
Explanation: Here we replace the occurrences of a specified substring 'Hello' within a string

Remove Substring Lists from String in Python

Below are the methods that we will cover in this article:

  • Using loop + replace()
  • Using replace() + join() + split()
  • Using split() Method, in and not-in Operators
  • Using regex and join() Function
  • Using NumPy
  • Using reduce() Function from functools Module

Remove substring list from String using loop + replace()

The combination of the above functions can be used to solve this problem. In this, we perform the replacement of multiple Strings as they occur using replace(). 

Python3




# 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

Time Complexity: O(n)
Auxiliary Space: O(n)

Remove Substring List from String using replace() + join() + split()

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

Python3




# 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

Remove Substring List from String using split() Method, in and not in Operators

In this method, we will Remove the substring list from the String using the split() Method, in and not in Operators.

Python3




# 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)

Remove Substring List From String using regex and join() Function

Import the “re” module for regular expression operations then initialize the original string “test_str” to “gfg is best for all geeks” Now initialize the list of substrings to be removed “sub_list” to [“best”, “all”] and then join the substrings in “sub_list” using the “map” and “join” functions and escape any special characters using “re.escape” function to create a pattern string “pattern”.Use the regular expression pattern string to substitute the substrings with an empty string using the “re.sub()” function and assign the result back to “test_str”

Print the resulting string after substring removal using the “print()” function.

Python3




import re
 
# 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 regex and join() function
pattern = '|'.join(map(re.escape, sub_list))
test_str = re.sub(r'\b(?:{})\b'.format(pattern), '', test_str)
 
# printing result
print("The string after substring removal : " + test_str)
#This code is contributed by Vinay Pinjala.


Output

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

The time complexity of the above code is O(NM), where N is the length of the input string and M is the length of the longest substring in the sub_list. This is because for each substring in the sub_list, we are performing a search and replace operation in the input string.

The auxiliary space is also O(N), as we are creating a new string object to store the modified version of the input string.

Remove Substring List from String Using Numpy

Create a NumPy array sub_arr from the sub_list same with array_test create a NumPy array test_arr from the words of test_str now create a boolean mask array mask_arr that is initialized to True for all indices then for each substring in sub_arr, invert the boolean mask for the indices where the substring is found in test_arr and in the end use the boolean mask array to select the remaining words from test_arr and join them using the join method to form the final string final_str.

Python3




import numpy as np
 
# initializing string
test_str = "gfg is best for all geeks"
 
# initializing sub list
sub_list = ["best", "all"]
 
# Remove substring list from String using numpy and string functions
sub_arr = np.array(sub_list)
test_arr = np.array(test_str.split())
mask_arr = np.ones(len(test_arr), dtype=bool)
for i in range(len(sub_arr)):
    mask_arr &= ~(test_arr == sub_arr[i])
 
# Join the remaining words in the array to form the final string
final_str = ' '.join(test_arr[mask_arr])
 
# printing original string
print("The original string is : " + test_str)
 
# printing result
print("The string after substring removal : " + final_str)
#This code is contributed by Rayudu.


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

Time Complexity: O(n), where n is the length of the input string. This is because the split() method has to traverse the entire input string to split it into words, and the loop that removes the substrings from the test_arr array iterates over the length of the sub_arr array.

Space Complexity: O(n), where n is the length of the input string. This is because the test_arr and mask_arr NumPy arrays are created from the words of the input string, and the mask_arr array has the same length as the test_arr array.

Remove Substring List from String Using reduce() Function from Functools Module

In this method, we will Remove the substring list from the String using reduce() function from functools module.

Python3




from functools import reduce
 
# 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 reduce() function from functools module
res = reduce(lambda s, sub: s.replace(sub, ""), sub_list, test_str)
 
# 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), where n is the length of the original string
Auxiliary space: O(n), where n is the length of the original string (to store the result string)

Remove a Substring from the String

Let’s First remove a part of the String(substring) then we will remove a list of substrings from the String.

To remove a substring from a string using the replace function, you can simply replace the substring with an empty string.

Input: input_string = "Hello World!"
modified_string = input_string.replace(" World!"," ")
Output: Hello
Explanation: In this case,only the first occurrence of " World" is removed, leaving the second occurrence intact.


Similar Reads

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 this method, we are using string slicing to remove th
3 min read
Python - Remove after substring in String
Given a String, remove all characters after a particular substring. Input : test_str = 'geeksforgeeks is best for geeks', sub_str = "for" Output : geeksforgeeks is best for Explanation : everything removed after for. Input : test_str = 'geeksforgeeks is best for geeks', sub_str = "is" Output : geeksforgeeks is Explanation : everything removed after
7 min read
Python - Filter the List of String whose index in second List contains the given Substring
Given two lists, extract all elements from the first list, whose corresponding index in the second list contains the required substring. Examples: Input : test_list1 = ["Gfg", "is", "not", "best", "and", "not", "CS"], test_list2 = ["Its ok", "all ok", "wrong", "looks ok", "ok", "wrong", "thats ok"], sub_str = "ok" Output : ['Gfg', 'is', 'best', 'an
10 min read
Python - Remove front K characters from each string in String List
Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus. Let’s discuss certain ways in which this can
6 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 certain way outs to solve this problem. Method #1: Usin
4 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python - Remove keys with substring values
Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove keys whose values have substring as argument we pass. This problem can occur in cases of web development and day-day programming. Lets discuss certain ways in which this task can be performed. Input : test_dict = {1 : 'Gfg is best for geeks'} sub_lis
6 min read
Python | Filter list of strings based on the substring list
Given two lists of strings string and substr, write a Python program to filter out all the strings in string that contains string in substr. Examples: Input : string = ['city1', 'class5', 'room2', 'city2']substr = ['class', 'city']Output : ['city1', 'class5', 'city2'] Input : string = ['coordinates', 'xyCoord', '123abc']substr = ['abc', 'xy']Output
8 min read
Python | Get the substring from given string using list slicing
Given a string, write a Python program to get the substring from given string using list slicing. Let’s try to get this using different examples. What is substring? A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the index of a substring and more. Syntax of list slicin
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 this can be performed. Method #1 : Using list com
5 min read
Practice Tags :