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):]
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
GeeksforGeeks
geeksforgeeks.org › python-string-removeprefix-function
Python String - removeprefix() function - GeeksforGeeks
December 5, 2024 - We can remove a prefix string from a string using Python String removeprefix() Method.
Learn by Example
learnbyexample.github.io › tips › python-tip-10
Python tip 10: removeprefix and removesuffix string methods
May 11, 2022 - On the other hand, the strip methods treat the argument as a set of characters to be matched any number of times in any order until a non-matching character is found. Here are some examples: >>> 'these memes'.removesuffix('esm') 'these memes' >>> 'these memes'.rstrip('esm') 'these ' >>> 'effective'.removeprefix('ef') 'fective' >>> 'effective'.lstrip('ef') 'ctive' ... See also my 100 Page Python Intro ebook.
Note.nkmk.me
note.nkmk.me › home › python
Remove a Substring from a String in Python | note.nkmk.me
April 23, 2025 - This article explains how to remove a substring (i.e., a part of a string) from a string in Python. Contents · Remove a substring by replacing it with an empty string · Remove exact match string: replace() Remove substrings using regex: re.sub() Remove leading and/or trailing characters · Remove leading and trailing characters: strip() Remove leading characters: lstrip() Remove trailing characters: rstrip() Remove prefix: removeprefix() (Python 3.9 or later) Remove suffix: removesuffix() (Python 3.9 or later) Remove a substring by position and length: slicing ·
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › str.removeprefix
A Friendly Guide to Python's removeprefix(): Pitfalls and Sample Code
The removeprefix() method was introduced in Python 3.9. It removes a specified prefix string from the start of the original string, if the prefix is present. If the string doesn't start with the given prefix, it returns the original string unchanged. # Basic Usage Example data = "file_report_2023.txt" cleaned_data = data.removeprefix("file_") print(cleaned_data) # Output: report_2023.txt data_no_prefix = "data_2023.txt" cleaned_data_no_prefix = data_no_prefix.removeprefix("file_") print(cleaned_data_no_prefix) # Output: data_2023.txt (Original string returned)
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 - $ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original string whose prefix needs to be removed. >>> my_string = "PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig" >>> >>> ## The partition() method splits the original string at the separator “=”. In this >>> ## example, this separator is the first and only occurrence.
Codecademy
codecademy.com › docs › python › strings › .removeprefix()
Python | Strings | .removeprefix() | Codecademy
August 28, 2025 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. ... A new string with the prefix removed if it exists, otherwise, the original string. In this example, a matching prefix is removed from a string, or the string is left unchanged if no match is found:
Kieran Barker
barker.codes › blog › removing-prefixes-and-suffixes-in-python
Removing prefixes and suffixes in Python - Kieran Barker
January 16, 2023 - If so, I get the length of the prefix variable’s value. I use this as the start value for the slice, i.e. the index of the first character that should be included in the new string. I create a slice of the url variable’s value starting from this index. Because I don’t specify a stop value, the slice continues until the end of the string. Finally, I reassign the url variable with the new value. To remove the suffix, I check if the value of the url variable ends with the value of the suffix variable.
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.'
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. I did some digging and, via a mention of the method in Stack Overflow, I learned that Python 3.9 added support for methods for removing prefixes and suffixes from strings: removeprefix and removesuffix.
Tutorialspoint
tutorialspoint.com › python › removeprefix_method.htm
Python String removeprefix() Method
text = "Hello World" result = text.removeprefix("Hello ") print(result) ... This example shows that if the given prefix does not exist in the specified string, the original string is returned without any modifications −
Real Python
realpython.com › lessons › removing-prefixes-suffixes
Removing Prefixes and Suffixes (Video) – Real Python
For example, if you want to remove the file extension, call filename.removesuffix(".txt"), leaving you with simply txt_transcript. 01:52 Again, if the suffix isn’t found, nothing will happen.
Published: September 23, 2025
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!