# Python | Remove substring list from String

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

• 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.
```

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next