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):]
Python
peps.python.org › pep-0616
PEP 616 – String methods to remove prefixes and suffixes | peps.python.org
March 19, 2020 - Within the tuple, only the first matching affix would be removed. This was rejected on the following grounds: This behavior can be surprising or visually confusing, especially when one prefix is empty or is a substring of another prefix, as in 'FooBar'.removeprefix(('', 'Foo')) == 'FooBar' or 'FooBar text'.removeprefix(('Foo', 'FooBar ')) == 'Bar text'.
regex - How to remove the prefix from each element of python list? - Stack Overflow
I have a python list with items as follows: [ 1.1 ] 1. a electronic bill presentment system. [ 1.2 ] a network. [ 1.3 ] a plurality of first stations, each associated with a respective one of a More on stackoverflow.com
Finally a remove prefix, sufix string method in Python 3.9
This strikes me as bloat since replace() and rfind() can be used already. related to user confusion about the existing str.lstrip and str.rstrip methods This is a user issue, not a language issue. It is stated quite clearly in documentation what those functions do. Why add in a crutch because people can't read? 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" https://docs.python.org/3/library/stdtypes.html#str.rstrip Again, LITERALLY STATES: "The chars argument is not a suffix; rather, all combinations of its values are stripped" several users on Python-Ideas [2] reported frequently including similar functions in their code for productivity. The implementation often contained subtle mistakes regarding the handling of the empty string, so a well-tested built-in method would be useful. Again, that's a user issue and not a language issue in my opinion. or to use regular expressions as in the expression re.sub('' + re.escape(prefix), '', s) Really ?! More on reddit.com
regex - Remove prefix from name python - Stack Overflow
names = [ 'LIC. SEBASTIÁN LASTIRI', 'ING. AGR. ROBERTO DANIEL RODRÍGUEZ', 'C.P.N. JULIO DOMINGO BURAK', 'INGENIERO HIDRÁULICO VÍCTOR AGUSTÍN PORRINO' ] I have such list with names, i need to remove More on stackoverflow.com
how to remove part of a file name recursively in python?
What you want to do is remove the prefix. Your prefix is 31 chars >>> len("The.Simpsons.The.Simpsons.clear") 31 >>> t="The.Simpsons.The.Simpsons.clearThe.SimpsonsSeason 1 EP7 The Call of the Simpsons.mkv" >>> t[31:] 'The.SimpsonsSeason 1 EP7 The Call of the Simpsons.mkv' A bit of advice: before you do something permanent like renaming files, print out new names just to make sure they look ok. More on reddit.com
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 - After verifying that the prefix exists, we slice the string to contain everything but the prefix and return the results. This gives the following output where the prefix "xy" is removed from the string: $ python remove_prefix_alt.py Before: xyxyxyxyxy | yzyzyzyzyz 5 After: xyxyxyxy | yzyzyzyzyz 4
GeeksforGeeks
geeksforgeeks.org › python-remove-prefix-strings-from-list
Python | Remove prefix strings from list - GeeksforGeeks
April 11, 2023 - In this, we don't perform removal in place, instead, we recreate the list without the elements that match the prefix. ... # Python3 code to demonstrate working of # Remove prefix strings from list # using list comprehension + startswith() # initialize list test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best'] # printing original list print("The original list : " + str(test_list)) # initialize prefix pref = 'x' # Remove prefix strings from list # using list comprehension + startswith() res = [ele for ele in test_list if not ele.startswith(pref)] # printing result print("List after removal of Kth character of each string : " + str(res))
Learn by Example
learnbyexample.github.io › tips › python-tip-10
Python tip 10: removeprefix and removesuffix string methods
May 11, 2022 - # remove 'sp' if it matches at the start of the input string >>> 'spare'.removeprefix('sp') 'are' # 'par' is present in the input, but not at the start >>> 'spare'.removeprefix('par') 'spare' # remove 'me' if it matches at the end of the input string # only one occurrence of the match will be removed >>> 'this meme'.removesuffix('me') 'this me' # characters have to be matched exactly in the same order >>> 'this meme'.removesuffix('em') 'this meme'
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.
Top answer 1 of 4
3
Here's one approach using re.sub:
import re
l = ['[ 1.1 ] 1. a electronic bill presentment system.','[ 1.2 ] a network.']
[re.sub(r'\[\s*\d+\.*\d*\s*\]\s+(?:\d+\.\s*)?', '', s) for s in l]
# ['a electronic bill presentment system.', 'a network.']
See demo
Testing with a larger list of strings:
l = ['[ 1.1 ] 1. a electronic bill presentment system.',\
'[ 1.2 ] a network.',\
'[ 1.3 ] a plurality of first stations, each associated with a respective one of a plurality of users and operable to transmit first requests for bills of its associated user via the network.',\
'[ 1.5 ] a plurality of second network stations, each associated with a respective one of a plurality of billers, configured to receive the transmitted second requests for bills and to transmit, responsive thereto, the requested bills of the associated user via the network.',\
'[ 1.6 ] wherein the bill availability information for the associated user identifies those of the plurality of billers having a bill available for that user without identifying an amount of the bill of each of the identified billers for the associated user.',\
'[ 2.1 ] 2. a method for presenting electronic bills.']
[re.sub(r'\[\s*\d+\.*\d*\s*\]\s+(?:\d+\.\s*)?', '', s) for s in l]
['a electronic bill presentment system.',
'a network.',
'a plurality of first stations, each associated with a respective one of a plurality of users and operable to transmit first requests for bills of its associated user via the network.',
'a plurality of second network stations, each associated with a respective one of a plurality of billers, configured to receive the transmitted second requests for bills and to transmit, responsive thereto, the requested bills of the associated user via the network.',
'wherein the bill availability information for the associated user identifies those of the plurality of billers having a bill available for that user without identifying an amount of the bill of each of the identified billers for the associated user.',
'a method for presenting electronic bills.']
2 of 4
2
You should regular expression to substitute the pattern with an empty string
>>> re.sub(r'\[\s?\d\.\d\s?]\s?(\d(\.\s)?)?', '', '[ 1.1 ] 1. a electronic bill presentment system.')
'a electronic bill presentment system.'
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....
Finxter
blog.finxter.com › home › learn python blog › how to remove the prefix of a string in python?
How To Remove The Prefix Of A String In Python? - Be on the Right Side of Change
March 23, 2021 - For example, my_string.removeprefix('xxx') removes the prefix 'xxx' from my_string. >>> 'xxxhello world'.removeprefix('xxx') 'hello world' Note: All the solutions provided below have been verified using Python 3.9.0b5
Top answer 1 of 3
1
Here is the solution for your issue:
import re
names = [
'LIC. SEBASTIÁN LASTIRI',
'ING. AGR. ROBERTO DANIEL RODRÍGUEZ',
'C.P.N. JULIO DOMINGO BURAK',
'INGENIERO HIDRÁULICO VÍCTOR AGUSTÍN PORRINO'
]
new_names = [re.sub("^\s+", "", i.split(".")[-1]) for i in names]
print new_names # [SEBASTIÁN LASTIRI', ROBERTO DANIEL RODRÍGUEZ', JULIO DOMINGO BURAK', 'INGENIERO HIDRÁULICO VÍCTOR AGUSTÍN PORRINO']
2 of 3
0
You can use the following code:
import re
names = [
'LIC. SEBASTIAN LASTIRI',
'ING. AGR. ROBERTO DANIEL RODRIGUEZ',
'C.P.N. JULIO DOMINGO BURAK',
'INGENIERO HIDRAULICO VICTOR AGUSTIN PORRINO'
]
for i in names:
res = re.split(r'\.\s*(?=[^.]+$)', i)
if len(res) > 1:
print res[1]
else:
print res[0]
Output:
SEBASTIAN LASTIRI
ROBERTO DANIEL RODRIGUEZ
JULIO DOMINGO BURAK
INGENIERO HIDRAULICO VICTOR AGUSTIN PORRINO