Remove a Substring from a String in Python

In this tutorial, we’ll tackle the different options you can use to remove a substring from a string in Python. You actually have countless options, but we’ll limit ourselves to the most common and practical ones.

Use str.replace() method to remove a substring from a string

Python comes with a series of useful methods that you can directly use without having to create your custom code. You can use str.replace() to replace a substring by a new one. If you add an empty string as a replacement, this method will actually remove the substring from the string.

old_string = "My sister is called Olivia" new_string = old_string.replace("Olivia","") print(new_string)
Code language: Python (python)

Output:

My sister is called

You can use a list comprehension to remove the same substring from a list of strings as well.

old_strings = ["mail.jpg","home.jpg","file.jpg","box.jpg"] new_strings = [string.replace('.jpg','') for string in old_strings] print(new_strings)
Code language: Python (python)

Output:

['mail', 'home', 'file', 'box']
Code language: JSON / JSON with Comments (json)

Use index to remove a substring from a string

If you know the index position of the substring you want to remove, you can also use this information to easily remove the substring from your string. Let’s use the same example as before, but with this new approach:

old_strings = ["mail.jpg","home.jpg","file.jpg","box.jpg"] new_strings = [string[:-4] for string in old_strings] print(new_strings)
Code language: Python (python)

Please note that when you use string[:-4], you’re basically removing the last four characters from a string. Which is why we want to achieve here.

Output:

['mail', 'home', 'file', 'box'
Code language: JavaScript (javascript)

Use RegEx to remove a substring from a string

Sometimes, our replacement logic is not straightforward, and we need to create a regular expression to do so. Luckily for us, Python comes with the re module to achieve just that.

import re old_string = 'A string with numbers: 12345' pattern = r'\d+' new_string= re.sub(pattern, '', old_string) print(new_string)
Code language: Python (python)

If you’re not familiar with regular expressions, please note that \d+ allows use to match any number composed of at least one digit.

Output:

A string with numbers:
Code language: JavaScript (javascript)

Use Pandas to remove a substring from a string

Lastly, you can use the Pandas library to remove a string from the substring. I recommend doing it like only if you are already working with a DataFrame. Otherwise, you’ll have to load your list into a Pandas Series, apply the change, and export to a list again. Which is far from being efficient.

import pandas as pd df = {'strings':['a1','b1','c1','d1']} df = pd.DataFrame(df, columns=['strings']) df['strings'] = df['strings'].str.replace('1','') print(df)
Code language: Python (python)

Output:

a
b
c
d

Leave a Reply

Your email address will not be published. Required fields are marked *