Remove Substring list from String using Python


Python is a very useful software used commonly by people spread all over the world to perform many different functions as per their personal need. It is used for many different purposes such as Data Science, Machine Learning, Web Development and to perform different processes with automation. It has many different features which help us to perform the above mentioned tasks but with so many features present within python, users have to face issues also. One such common issue faced by the user is to remove substring from a string. Many−a−times multiple substrings are also to be removed from one main string. In this article we are going to learn how to remove substring list from string using python.

Different Methods to Remove Substrings

Replace Function

This is a very simple method of removing substring from string. With the help of the replace() function and simply defining the string to be kept and the substring to be removed, we can easily remove the unwanted substrings. Let’s take an example to make it clearer: -

def extra_substrings(main_string, remove_substrings): #Defining the format of string and substring
    for remove_substring in remove_substrings:
        main_string = main_string.replace(remove_substring, "")  #For any substring having substring within it
    return main_string

Example

def extra_substrings(main_string, remove_substrings): #Defining the format of string and substring
    for remove_substring in remove_substrings:
        main_string = main_string.replace(remove_substring, "")  #For any substring having substring within it
    return main_string
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]  #These are the substrings which are to be removed with the help of replace() function
new_String = extra_substrings(whole_string, remove_substrings) #The extra_substring checks each of the defined substrings and removes them from the string
print(new_String)

Output

The output of the above code will be as follows:

Hello, ! This is a  string  for example.  

re Module

In this procedure, the substring will be taken out of the main text using the re module. Python uses the re module to work with regular expressions. To define the substrings and eliminate them from the strings, we will design a pattern using the re.sub() method of the re module. The code of this method will be as follows:

import re  #Do not forget to import re or else the code will not run correctly

def extra_substrings(main_string,remove_substrings):  #The string and substring are taken as argument by extra_substring
    pattern = "|".join(map(re.escape, remove_substrings)) #The | will act as a separator in the pattern that is defined
    return re.sub(pattern, "", main_string)   # The re.sub() function will be used to replace all the substring in the pattern with an empty place

Example

Let’s take an example for the above code to make it more clear:

import re  #Do not forget to import re or else the code will not run correctly

def extra_substrings(main_string,remove_substrings):  #The string and substring are taken as argument by extra_substring
    pattern = "|".join(map(re.escape, remove_substrings)) #The | will act as a separator in the pattern that is defined
    return re.sub(pattern, "", main_string)   # The re.sub() function will be used to replace all the substring in the pattern with an empty place
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substrings)  #The argument will remove all the words defined within substrings
print(new_string)

Output

The output of the above code will be as follows:

Hello, ! This is a string for example.

List Comprehension

Another extremely straightforward technique for removing the substrings from the main string is this one. We can provide the function the string and substring arguments before defining the substrings. Each component of the main text will be examined by the list comprehension, and any substrings found in the code will be eliminated. The code for this method will be as follows:

def extra_substrings(main_string, remove_substrings): 
    words = main_string.split()  # Split the string into words
    useful_words = [word for word in words if all(sub not in word for sub in remove_substrings)]   #With the help of all() function, list comprehension will check all the elements in the string and all the defined substrings will be removed
    return ' '.join(useful_words)

Example

Let’s take an example using the above code to make it more clear:

def extra_substrings(main_string, remove_substrings): 
    words = main_string.split()  # Split the string into words
    useful_words = [word for word in words if all(sub not in word for sub in remove_substrings)]   #With the help of all() function, list comprehension will check all the elements in the string and all the defined substrings will be removed
    return ' '.join(useful_words)
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substring = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substring)
print(new_string)

Output

The output of the above code will be as follows:

Hello, ! This is a string for example.

Translate Function

We will use the translate function in this method to remove the substrings from the main strings. The translate functions returns to the string where the specified elements mentioned in the translational table are present and replace them with an empty string. The code to create the translational table to remove the substrings is as follows:

def remove_substrings_translate(main_string, remove_substrings):
    translation_table = str.maketrans("", "", "".join(remove_substrings)) #str.maketrans() is used to create the translational table 
    return main_string.translate(translation_table)  #str.translate() is used to remove the substrings with the help of translational table

Example

Let’s take an example with the above code to understand it more clearly:

def remove_substrings_translate(main_string, remove_substrings):
    translation_table = str.maketrans("", "", "".join(remove_substrings)) #str.maketrans() is used to create the translational table 
    return main_string.translate(translation_table)  #str.translate() is used to remove the substrings with the help of translational table
whole_string = "Hello, world! This is a sample string."
remove_substrings = ["world", "sample"]
new_string = remove_substrings_translate(whole_string, remove_substrings)
print(new_string)

Output

The output of the above code will be as follows:

H, ! Thi i   ting.

re module with function

This is a complex method used in cases when the user requires more flexibility. We will use re.sub() function and create another personalised custom function which allows us to decide the substring to be replaced. The code to use re.sub() along with a custom function will be as follows:

import re #Do not forget to import re or else error might occur

def extra_substrings(main_string, remove_substrings):  #Defining the arguments
    pattern = "|".join(map(re.escape, remove_substrings))
    
    def replacement(match): #Custom Function to define the substring with an empty string
        return ""
    
    return re.sub(pattern, replacement, main_string)  #re.sub() to remove the substring defined by custom function replacement()

Example

Let’s take an example with the above code to understand it more clearly:

import re #Do not forget to import re or else error might occur

def extra_substrings(main_string, remove_substrings):  #Defining the arguments
    pattern = "|".join(map(re.escape, remove_substrings))
    
    def replacement(match): #Custom Function to define the substring with an empty string
        return ""
    
    return re.sub(pattern, replacement, main_string)  #re.sub() to remove the substring defined by custom function replacement()
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substrings)
print(new_string)

Output

The output of the above code will be as follows:

Hello, ! This is a string for example.

Conclusion

The process of removing substring from a string can be prove to be frustrating many−a−times if the correct approach is not followed by the user. This is a very commonly faced issue by the user and hence the correct steps must be followed. One can refer the different methods given in the above article to remove substrings from the main string using python.

Updated on: 01-Aug-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements