Remove substring from string in Python if exist

Do you want to Remove the substring from the string in Python? In this Python tutorial, you will learn multiple ways to remove substrings from the string with practical examples and realistic scenarios.

While working on a project, I stored the data in a text file. When I needed to remove some unnecessary words and phrases from the text file, these methods helped me remove substrings from the string in Python.

So, I thought I would share all the different methods for removing the substring from a string in Python.

6 ways to remove substrings from string if exist in Python

Let’s understand the scenario clearly so you can use the method in the correct situation.

So there is text like this,

text = "Welcome to Python Guides...you can learn Python from basic to advance"

remove = "...you can learn Python from basic to advance"

Now I need to remove some phrases from the text and need this as an output,

Welcome to Python Guides

In this scenario, you can use all the approaches we will explain.

Python remove the substring from the string if it exists using the replace() method

First, we will use the replace() method to remove a substring from a string in Python. Here, we will replace the given substring pattern with an empty string. This method does not affect the original string.

Syntax

var_name.replace(old_string_pattern, new_string)
  • In the above syntax, the “var_name” should be a string datatype only.
text = "British colonization led to the first settlement of the Thirteen Colonies"
updated_string = text.replace("of the Thirteen Colonies", "")
print(updated_string)
Python remove substring from string if exists using replace() method

In the above code, we have a variable named state that contains a string. We need to remove the word “State” from the string, so we replaced “State” with “Empty string” like this: ‘state.replace(” State”, ” “)’ using a method to remove the substring in Python and store it in the updated_string variable.

READ:  How to convert a list to DataFrame in Python [9 ways]

Remove substring from string Python using split() and join()

We will use the join() and split() methods to remove the substring from Python’s string. The join() method is used to concatenate strings in Python.

The split() method separates all the words based on the given separator value and creates a list of all the words in the string.

Syntax of join() method

string.join(["str1" , "str2", ... ])
  • In syntax, you can also give a direct string like this join(“Str”), but the join() method can also concat a list of strings.

Syntax of split() method

string.split("seperator")
  • string.split(“separator“): split() method will be used for string datatype only. If you don’t give value to the separator, it will take white space as a default value
text = "The United States has had the largest nominal GDP in the world"

updated_text = " ".join(text.split('has had the largest nominal GDP in the world'))
print(updated_text)
Remove substring from string Python using split() and join()

In the above code, we have a string, and we need to remove some part of the string, so we are using the split() method to separate the needed string part so it will look like this
[‘The United States ‘, ”]” and then it will concat to updated_text using join() method.

How to remove substring from string in Python using List Comprehensions

We will take the same approach as in the previous example, but this time, we will separate the string to the list based on the commas. Then, we will use list comprehension to iterate over every list element.

List comprehension can be a better approach than using the normal for loop of Python.

Let’s see how Python remove a substring from a string if it exists using list comprehension.

states = "Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut"

remove_state = "Alaska"
states = ", ".join([state for state in states.split(", ") if state != remove_state])
print(states)
How to remove substring from string in Python using List Comprehensions

In the above code, we use a for loop after splitting the string values, and then we put the condition “if state != remove_state” to include the elements that do not match the remove_state value.

READ:  Find Largest and Smallest Number in Python Without List

Python remove substring if exists using Regular Expressions (regex)

We can also use Regular Expression, also known as regex in Python, to remove the substrings of string using the re.sub() regex method in Python.

The re.sub() method will also work like the replace() method used in the first example.

Syntax

re.sub(pattern, replce pattern, input_string)
  • Before using re.sub() method, you have to import the re module.
  • re.sub(pattern, replace pattern, input_string): In the pattern, you will give the targeted perhaps which you want to replace, and in replace_pattern, you can give another string you wish to include.
  • Input_string will be the main string from where you want to make changes.
import re

string = "The capital of California is Sacramento, and the capital of New York is Albany."
string = re.sub(', and the capital of New York is Albany', '', string)
print(string)
Python remove substring if exists using Regular Expressions (regex)

In the above code, we are using the re.sub() method to remove the substring of the string if it exists. So we gave an empty string in the replace string parameter “re.sub(‘, and the capital of New York is Albany’, ”, string)” so it will remove the pattern you’ve given in the first parameter.

Python remove substring from string using String Slicing

Now, we will use string slicing in Python. It is a process of extracting the elements by giving the range of the index position. Also, we will use the find() method in Python to get the index value of the given string.

Syntax of String slicing in Python

string[start_index : end_index]
  • string[start_index : end_index]: If you don’t provide anything at index positions, it will select the whole string by default.
  • Index positions should always be integer values; otherwise, it will give an error.
str = "Washington D.C., the capital of the USA"

index = str.find(", the capital of the USA")
str = str[:index]
print(str)
Python remove substring from string using String Slicing

In the above code, we’ve initialized a variable named index and assigned the index position of the element using the find() method, which we need to remove from the string.

READ:  How to Replace Whitespaces with Underscore in Python [5 Methods]

After getting the index position of the removable value, extract the required text and update the original string using str = str[:index].

How to remove a substring from a string in Python using the lstrip() and rstrip() methods

We can also use the lstrip() and rstrip() methods to remove the substring, but the condition is that the removable string should be at the start index or the last index. Only then can it remove the string; otherwise, it will give the exact string as it is without updating it.

Remove a substring from a string in Python using the lstrip() method

The lstrip () method removes the elements to the left of the string at the start index position.

Syntax

string.lstrip('value_to_be_remove')
  • string.lstrip(‘value_to_be_remove’): If you don’t give any value in the parameter, then it will remove the white space at the start index if it is there.
state = "state name is Washington, also known as evergreen state"
new_state = state.lstrip('state')
print(new_state)
Remove a substring from a string in Python using the lstrip() method

The above code has a string that includes the state word at the start and end. So, if you need to remove the first word only on the left side, you can use the lstrip() method, like this: “state.lstrip(‘state’)”

Python Remove Substring From String If Exists using rstrip() method

The rstrip () method removes the elements to the right of the string at the end index position.

state = "The state name is Washington, also known as evergreen state"
new_state = state.rstrip('state')
print(new_state)
Python Remove Substring From String If Exists using rstrip() method

In the above, we took the same example as the previous one, but here we are removing the pattern “new_state = state.rstrip(‘state’)“, which is at the last index position.

Conclusion

In this Python article, you learned how to remove a substring from a string if it exists. We explored 6 different methods and techniques, including the replace() method, list comprehension, regex, and more, with practical examples for every method.