Python removeprefix() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
clean = text.removeprefix(prefix)
CallWhat 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
python
str.removeprefix(prefix) -> str
# returns: a new string without the prefix, or an unchanged copy
# added in Python 3.9

Parameters

ParameterDescription
prefixRequired. The exact text to remove from the start. It must be a string. An empty prefix removes nothing.

Return Value

Return ValueDescription
strA new string without the prefix if the string started with it, otherwise a string equal to the original. The original is never changed.

Python Examples

Example 1 — Drop a Known Prefix

Remove a prefix that is there, one that is missing, and see that only one copy is removed.

python
name = "test_login"

print(name.removeprefix("test_"))
print(name.removeprefix("prod_"))
print("zzzdone".removeprefix("z"))
print(name)

How It Works

  1. An exact match. The name begins with test_, so those five characters are removed.
  2. No match, no change. prod_ is not at the start, so the same text comes back.
  3. Only once. "zzzdone" loses a single z, not all three.
  4. 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.

python
fruit = "banana"

print(repr(fruit.lstrip("ban")))
print(fruit.removeprefix("ban"))
print(fruit.removeprefix("ban") == "ana")

How It Works

  1. lstrip() uses a set. The characters b, a, and n are all in the set, so every character of banana is removed.
  2. Nothing is left. The result is an empty string, and repr() shows it as two quote marks.
  3. 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.

python
commands = ["cmd_start", "cmd_stop", "help", "cmd_cmd_run"]
names = [item.removeprefix("cmd_") for item in commands]

print(names)

How It Works

  1. One call for each item. The comprehension runs removeprefix("cmd_") on every command.
  2. Items without the prefix stay. help does not begin with cmd_, so it is copied unchanged.
  3. Only the first copy goes. cmd_cmd_run becomes cmd_run, which shows that the prefix is removed just once.

Frequently Asked Questions

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.
Python 3.9. On older versions use text[len(prefix):] when text.startswith(prefix) is True.
removeprefix() removes one exact piece of text. lstrip() removes any characters from a set, as many times as they appear at the start.
No. It removes the prefix a single time, so "aaab".removeprefix("a") is "aab".
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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