Skip to main content

How to Remove a Prefix from a String in Python with removeprefix

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Turning INV-1001 into 1001 means removing a known prefix from the start of a string. Since Python 3.9, strings have a method for exactly that, removeprefix, which also leaves strings without the prefix alone. This guide runs it on a few invoice numbers and shows why lstrip, which looks similar, can remove too much. Real output from Python 3.15.2.

Syntax​

Syntax
new_text = text.removeprefix(prefix)
PartWhat it means
textThe string to look at
prefixThe exact text to take off the start
new_textA new string: text without prefix when it starts with it, otherwise a copy of text

Python's reference for str.removeprefix: "If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string." It was "Added in version 3.9."

Step 1: Remove the Prefix​

The example takes INV- off a list of invoice numbers, then tries three harder cases: the prefix twice, a lower-case prefix, and a word that only starts like the prefix, which it also gives to lstrip:

The remove_prefix.py script in a macOS-style code window: a loop prints three invoice numbers with removeprefix INV- applied, then removeprefix on INV-INV-1004 and inv-1005, then INVOICE-77 with removeprefix and with lstrip.

remove_prefix.py
invoices = ["INV-1001", "INV-1002", "1003"]

for invoice in invoices:
print(invoice.removeprefix("INV-"))

print("INV-INV-1004".removeprefix("INV-"))
print("inv-1005".removeprefix("INV-"))

print("INVOICE-77".removeprefix("INV-"))
print("INVOICE-77".lstrip("INV-"))
Terminal
uv run python remove_prefix.py
Output
1001
1002
1003
INV-1004
inv-1005
INVOICE-77
OICE-77

The script's output in a terminal: 1001, 1002, 1003, INV-1004, inv-1005, INVOICE-77 and OICE-77.

Line by line:

  • 1001, 1002: the prefix is gone.
  • 1003: the number never had the prefix, so it comes back unchanged, with no error.
  • INV-1004: only one copy of the prefix is removed.
  • inv-1005: unchanged, because the match is case-sensitive: inv- isn't INV-.
  • INVOICE-77: unchanged, because the word starts with INVO, not INV-.
  • OICE-77: lstrip treats "INV-" as a set of characters and removes every leading I, N, V and -, eating into the word.

The reference warns about that last one: lstrip's argument "is not a prefix; rather, all combinations of its values are stripped".

Common Mistakes​

MistakeWhat happensDo this instead
text.lstrip("INV-") to remove a prefixEvery leading I, N, V and - goes: INVOICE-77 becomes OICE-77text.removeprefix("INV-")
Calling it without keeping the resultStrings never change in place, so the old text staysinvoice = invoice.removeprefix("INV-")

Troubleshooting​

SymptomLikely causeFix
The prefix staysDifferent case, or a space before itMatch the exact text, such as text.strip().removeprefix("INV-")
More than the prefix disappearedlstrip instead of removeprefixUse removeprefix

FAQ​

Is there one for the end of a string?​

Yes: removesuffix, which the reference lists next to it. "report.csv".removesuffix(".csv") gives report.

How do I know whether the prefix was there?​

Check with text.startswith(prefix) first, which returns True or False; the reference points to it from removeprefix.

Conclusion​

"INV-1001".removeprefix("INV-") returned 1001, while numbers without the prefix, a lower-case prefix and a word that only starts like it came back unchanged; lstrip("INV-") turned INVOICE-77 into OICE-77, which is why it's the wrong tool for a prefix.

Additional Resources​