Remove a prefix from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Remove a prefix from a String in Python #

Use the str.removeprefix() method to remove a prefix from a string, e.g. without_prefix = my_str.removeprefix('__'). The removeprefix() method will return a new string with the specified prefix removed.

main.py
my_str = '!@#dev test ship___' # ✅ Remove prefix from string (Python 3.9+) without_prefix = my_str.removeprefix('!@#') print(without_prefix) # 👉️ 'dev test ship___' without_suffix = my_str.removesuffix('___') print(without_suffix) # 👉️ '!@#dev test ship' # ----------------------------------------------------- # ✅ Remove prefix from string (older than Python 3.9) def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string print(removeprefix('@@avocado', '@@')) # 👉️ 'avocado' print(removeprefix('!!banana', '!!')) # 👉️ 'banana'

The first example uses the str.removeprefix() method to remove a prefix from a string.

The method is available in Python version 3.9 or newer.

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
print('_hello'.removeprefix('_')) # 👉️ '_' print('???abc'.removeprefix('???')) # 👉️ '???'

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

If you use an older version of Python, you can implement the removeprefix method yourself.

main.py
def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string print(removeprefix('@@avocado', '@@')) # 👉️ 'avocado' print(removeprefix('!!banana', '!!')) # 👉️ 'banana'

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

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

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

main.py
def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string print(removeprefix('...mango', '...')) # 👉️ 'mango' print(removeprefix('>>>melon', '>>>')) # 👉️ 'melon'
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.

If you need to remove a prefix from a string, ignoring the case, convert the prefix and the string to lowercase.

main.py
def removeprefix(string, prefix): if string.lower().startswith(prefix.lower()): return string[len(prefix):] return string print(removeprefix('aBCmango', 'abc')) # 👉️ 'mango' print(removeprefix('tESTmelon', 'test')) # 👉️ 'melon'

We used the str.lower() method to convert the string and the prefix to lowercase.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

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.