Remove the Prefix and Suffix from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the Prefix and Suffix from a String in Python #

Use the str.removeprefix() and str.removesuffix() methods to remove the prefix and suffix from a string. The methods take the prefix and suffix as parameters and remove them from the string.

main.py
# requires Python 3.9+ my_str = '$123apple banana kiwi#456' without_prefix = my_str.removeprefix('$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = my_str.removesuffix('#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = my_str.removeprefix('$123').removesuffix('#456') # 👇️ 'apple banana kiwi' print(without_prefix_and_suffix)
The str.removeprefix and str.removesuffix methods require Python version 3.9+. If you use an older version, scroll down to the next subheading.

The str.removeprefix method checks if the string starts with the specified prefix and if it does, the method returns a new string excluding the prefix, otherwise it returns a copy of the original string.

main.py
my_str = '$123apple banana kiwi#456' without_prefix = my_str.removeprefix('$123') print(without_prefix) # 👉️ 'apple banana kiwi#456'

The method does not change the original string, it returns a new string. Strings are immutable in Python.

If you only need to remove the suffix from the string, use the str.removesuffix method.

main.py
my_str = '$123apple banana kiwi#456' without_suffix = my_str.removesuffix('#456') print(without_suffix) # 👉️ '$123apple banana kiwi'

The str.removesuffix method checks if the string ends with the specified suffix and if it does, the method returns a new string excluding the suffix, otherwise it returns a copy of the original string.

If you need to remove the prefix and suffix from the string, call both methods.

main.py
my_str = '$123apple banana kiwi#456' without_prefix_and_suffix = my_str.removeprefix( '$123' ).removesuffix('#456') # 👇️ 'apple banana kiwi' print(without_prefix_and_suffix)

Remove the Prefix and Suffix from a String for older Python versions #

If you use an older Python version (than 3.9), implement the removeprefix and removesuffix functions in the following way.

main.py
my_str = '$123apple banana kiwi#456' def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string without_prefix = removeprefix(my_str, '$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = removesuffix(my_str, '#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = removesuffix(without_prefix, '#456') print(without_prefix_and_suffix) # 👉️ 'apple banana kiwi'

The removeprefix function checks if the passed in string starts with the specified substring.

If the string doesn't start with the specified substring, the function returns the string as is.

If the string starts with the substring, the function returns a string slice starting at the character after the last character of the substring.

main.py
def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string
Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The string slice starts at the character after the prefix and grabs the rest of the string.

The removesuffix function is implemented in a similar way.

main.py
def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string

If the passed in string doesn't end with the specified substring, the function returns the string as is.

If the string ends with the substring, the function returns a slice of the function up to the specified substring.

We used negative string slicing to exclude the suffix from the new string slice.

Here is the complete code snippet.

main.py
my_str = '$123apple banana kiwi#456' def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string without_prefix = removeprefix(my_str, '$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = removesuffix(my_str, '#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = removesuffix(without_prefix, '#456') print(without_prefix_and_suffix) # 👉️ 'apple banana kiwi'
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.