How to Remove a Prefix from a String in Python with removeprefix
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
new_text = text.removeprefix(prefix)
| Part | What it means |
|---|---|
text | The string to look at |
prefix | The exact text to take off the start |
new_text | A 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:

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-"))
uv run python remove_prefix.py
1001
1002
1003
INV-1004
inv-1005
INVOICE-77
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'tINV-.INVOICE-77: unchanged, because the word starts withINVO, notINV-.OICE-77:lstriptreats"INV-"as a set of characters and removes every leadingI,N,Vand-, 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
| Mistake | What happens | Do this instead |
|---|---|---|
text.lstrip("INV-") to remove a prefix | Every leading I, N, V and - goes: INVOICE-77 becomes OICE-77 | text.removeprefix("INV-") |
| Calling it without keeping the result | Strings never change in place, so the old text stays | invoice = invoice.removeprefix("INV-") |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The prefix stays | Different case, or a space before it | Match the exact text, such as text.strip().removeprefix("INV-") |
| More than the prefix disappeared | lstrip instead of removeprefix | Use 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
- str.removeprefix — the method and its examples.
- str.lstrip — why its argument isn't a prefix.
