Home Python String Methods removeprefix() Python removeprefix() Method Definition removeprefix() removes one exact piece of text from the start of a string, but only if the string really begins with it. "test_login".removeprefix("test_") is "login". If the prefix is not there, you get the same text back.
It is the safe replacement for lstrip() when you mean a word and not a set of characters. It removes the prefix once , never more. The method was added in Python 3.9 , and it returns a new string.
text.removeprefix() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax Pass the prefix to remove. It is required, and it must be a string.
clean = text.removeprefix(prefix) Call What it does "test_login".removeprefix("test_")Returns 'login'. The exact prefix is removed. "hello".removeprefix("x")Returns 'hello'. The text does not start with x, so it is unchanged. "aaab".removeprefix("a")Returns 'aab'. Only one copy of the prefix is removed.
For the end of a string use removesuffix() . To strip a set of characters use lstrip() . To only test the start, use startswith() .
Library declarations str.removeprefix(prefix) -> str
# returns: a new string without the prefix, or an unchanged copy
# added in Python 3.9 Parameters Parameter Description prefixRequired. The exact text to remove from the start. It must be a string. An empty prefix removes nothing.
Return Value Return Value Description strA new string without the prefix if the string started with it, otherwise a string equal to the original. The original is never changed.
Important
removeprefix() needs Python 3.9 or later. It removes the prefix only once , and only as a whole. If you need to support older versions, write text[len(p):] if text.startswith(p) else text. Do not use lstrip(prefix) for this job, because it removes a set of characters.
Python Examples 1. Remove a prefix 2. removeprefix() or lstrip() 3. Clean a list of names Example 1 — Drop a Known Prefix Remove a prefix that is there, one that is missing, and see that only one copy is removed.
name = "test_login"
print(name.removeprefix("test_"))
print(name.removeprefix("prod_"))
print("zzzdone".removeprefix("z"))
print(name) login
test_login
zzdone
test_login How It Works An exact match. The name begins with test_, so those five characters are removed.No match, no change. prod_ is not at the start, so the same text comes back.Only once. "zzzdone" loses a single z, not all three.The original is unchanged. The last line still prints test_login.Example 2 — Why lstrip() Is Not the Same Remove the word ban from the start of banana in two ways.
fruit = "banana"
print(repr(fruit.lstrip("ban")))
print(fruit.removeprefix("ban"))
print(fruit.removeprefix("ban") == "ana") How It Works lstrip() uses a set. The characters b, a, and n are all in the set, so every character of banana is removed.Nothing is left. The result is an empty string, and repr() shows it as two quote marks.removeprefix() is exact. It removes only the three characters ban at the start and keeps ana, so the comparison is True.Example 3 — Remove a Command Prefix From a List Remove the prefix from every name that has it, and keep the others as they are.
commands = ["cmd_start", "cmd_stop", "help", "cmd_cmd_run"]
names = [item.removeprefix("cmd_") for item in commands]
print(names) ['start', 'stop', 'help', 'cmd_run'] How It Works One call for each item. The comprehension runs removeprefix("cmd_") on every command.Items without the prefix stay. help does not begin with cmd_, so it is copied unchanged.Only the first copy goes. cmd_cmd_run becomes cmd_run, which shows that the prefix is removed just once.Frequently Asked Questions What does removeprefix() do in Python? It returns a copy of the string without the given prefix if the string starts with it. If it does not, it returns the string unchanged.
Which Python version added removeprefix()? Python 3.9. On older versions use text[len(prefix):] when text.startswith(prefix) is True.
What is the difference between removeprefix() and lstrip()? removeprefix() removes one exact piece of text. lstrip() removes any characters from a set, as many times as they appear at the start.
Does removeprefix() remove the prefix more than once? No. It removes the prefix a single time, so "aaab".removeprefix("a") is "aab".
Does removeprefix() change the original string? No. Strings are immutable, so it returns a new string. Assign the result to keep it.
Next: removesuffix() Return a copy without the given suffix, if the string ends with it. Python 3.9+.
removesuffix() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion