For Python 3.9+:

text.removeprefix(prefix)

For older versions, the following provides the same behavior:

def remove_prefix(text, prefix):
    if text.startswith(prefix):
        return text[len(prefix):]
    return text
Answer from Elazar on Stack Overflow
๐ŸŒ
Python
peps.python.org โ€บ pep-0616
PEP 616 โ€“ String methods to remove prefixes and suffixes | peps.python.org
March 19, 2020 - This is a proposal to add two new methods, removeprefix() and removesuffix(), to the APIs of Pythonโ€™s various string objects. These methods would remove a prefix or suffix (respectively) from a string, if present, and would be added to Unicode ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-removeprefix-function
Python String - removeprefix() function - GeeksforGeeks
July 23, 2025 - Python String removeprefix() function removes the prefix and returns the rest of the string.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-remove-the-prefix-and-suffix-from-a-string
Python: Remove the Prefix and Suffix From a String
March 7, 2023 - This code removes the prefix "xy" of the first string and displays a number of occurrences of the prefix for both lines, at the end. Let's run the code: $ python remove_prefix.py Before: xyxyxyxyxy | yzyzyzyzyz 5 After: xyxyxyxy | yzyzyzyzyz 4
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .removeprefix()
Python | Strings | .removeprefix() | Codecademy
August 28, 2025 - The .removeprefix() method is a built-in string method that returns a new string with the specified prefix removed, if present. If the string does not start with the given prefix, the original string is returned unchanged.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-remove-prefix-strings-from-list
Python | Remove prefix strings from list - GeeksforGeeks
April 11, 2023 - ... # Python3 code to demonstrate working of # Remove prefix strings from list # using loop + remove() + startswith() # initialize list test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best'] # printing original list print("The original list ...
Find elsewhere
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ how to remove a prefix or suffix from a string in python
How to Remove a Prefix or Suffix from a String in Python โ€ข datagy
December 16, 2022 - How to use string slicing to remove a prefix or a suffix ... If you are using Python 3.9 or later, the best way to remove a suffix from a string in Python is to use the .removeprefix() method.
๐ŸŒ
James' Coffee Blog
jamesg.blog โ€บ 2026 โ€บ 06 โ€บ 22 โ€บ removing-prefixes-and-suffixes-in-python
Removing prefixes and suffixes in Python - with words, wonder
June 22, 2026 - If the string doesnโ€™t contain the prefix, nothing happens; if the string does contain the prefix, the prefix is removed. Note: If you are parsing URLs in Python, you should use a library like urllib.parse to extract parts of a URL.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ removeprefix_method.htm
Python String removeprefix() Method
The Python string removeprefix() method is used to remove a specified prefix from the beginning of a string. If the string starts with the specified prefix, the method removes the prefix from the string and returns the modified string.
๐ŸŒ
Kieran Barker
barker.codes โ€บ blog โ€บ removing-prefixes-and-suffixes-in-python
Removing prefixes and suffixes in Python - Kieran Barker
January 16, 2023 - Not only is this more readable, ... โ‰ค 3.8, the easiest way to remove a prefix (or suffix) from a string is to check if the string starts with the prefix (or ends with the suffix) and slice it....
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ prefixes-and-suffixes
Checking for string prefixes and suffixes in Python - Python Morsels
August 12, 2025 - To remove a prefix or suffix, use the removeprefix or removesuffix methods. If you need to remove repeated characters from either end of a string, check out the various strip methods instead.
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ finally a remove prefix, sufix string method in python 3.9
r/Python on Reddit: Finally a remove prefix, sufix string method in Python 3.9
April 21, 2020 - https://docs.python.org/3/library/stdtypes.html#str.lstrip ยท LITERALLY STATES: "The chars argument is not a prefix; rather, all combinations of its values are stripped"
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.removeprefix.html
pandas.Series.str.removeprefix โ€” pandas 3.0.5 documentation
Remove a suffix from an object series. ... >>> s = pd.Series(["str_foo", "str_bar", "no_prefix"]) >>> s 0 str_foo 1 str_bar 2 no_prefix dtype: str >>> s.str.removeprefix("str_") 0 foo 1 bar 2 no_prefix dtype: str
๐ŸŒ
Python Guides
pythonguides.com โ€บ remove-prefixes-from-strings-in-python
How To Remove Prefixes From Strings In Python?
March 19, 2025 - Learn how to remove prefixes from strings in Python using methods like `lstrip()`, `removeprefix()`, and slicing. Includes examples for string manipulation!
๐ŸŒ
Pythontic
pythontic.com โ€บ class โ€บ str โ€บ removeprefix
removeprefix() Method - Str Class | Pythontic.com
The method removeprefix() of str class in Python, removes a given prefix from a string and returns the modified string.
๐ŸŒ
DEV Community
dev.to โ€บ delta456 โ€บ python-removeprefix-and-removesuffix-34jp
Python: removeprefix() and removesuffix() - DEV Community
August 24, 2022 - def removeprefix(self: str, prefix: str, /) -> str: if self.startswith(prefix): return self[len(prefix):] else: return self[:] ... def removesuffix(self: str, suffix: str, /) -> str: if self.endswith(suffix): return self[:-len(suffix)] else: ...