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 Top answer 1 of 6
496
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
2 of 6
77
Short and sweet:
def remove_prefix(text, prefix):
return text[text.startswith(prefix) and len(prefix):]
00:18
Best Way To Remove Prefix and Suffix from string | #python - YouTube
08:30
partition Function | removeprefix | removesuffix | built in functions ...
Python String Remove Prefix - YouTube
00:59
Python 3 | String Prefix & Suffix Management #coding #programming ...
00:58
Remove prefix www. From links #python #funny - YouTube
Python Trick: Use str.removeprefix() and str.removesuffix() for ...
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 ...
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....
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
Top answer 1 of 2
3
Starting with Python 3.9 you can use str.removeprefix:
word = word.removeprefix(prefix)
For other versions of Python you can use:
if word.startswith(prefix):
word = word[len(prefix):]
2 of 2
2
Assuming you only want to remove the prefix from the first word and leave the rest alone, I see no reason to use a for loop. Instead, I would recommend this:
def prefix_removal(text):
first_word = text.split()[0]
if first_word.startswith(prefixes):
return text[len(prefixes):]
return text
Hopefully this answers your question, good luck!
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.