As a Python developer cleaning up text, you’ll often need to remove numbers from a string, like codes in product names. The re.sub() function does it in one line.
Quick answer: re.sub(r"\d+", "", text) deletes every digit and returns a new string.
In this tutorial, we’ll remove numbers from a string in Python with and without regex. We’ll also keep the digits in words like Route66.
What You Need to Remove Numbers from a String
We used Python 3.12.5. There’s nothing to install until pandas in Step 7.
Our sample is one product label: 4821 Route66 poster, 2 pack 2026. If you want to keep the numbers instead, extract the numbers from the string.
Step 1: Remove All Numbers with re.sub()
- In VS Code, paste this code into a new file named
remove_digits.pyand save it. - In the VS Code terminal, run
python remove_digits.py.
import re
item = "4821 Route66 poster, 2 pack 2026"
no_digits = re.sub(r"\d+", "", item)
print(repr(no_digits))
Output:
' Route poster, pack '

\d+ matches one or more digits in a row, and re.sub() replaces each match with nothing. Here’s more on the re.sub() function.
You still have extra spaces, and Route66 is now Route. Steps 2 and 4 fix both.
Step 2: Clean Up the Spaces the Numbers Leave
Split the result and join it again. Save this as clean_spaces.py:
import re
item = "4821 Route66 poster, 2 pack 2026"
no_digits = re.sub(r"\d+", "", item)
print(" ".join(no_digits.split()))
Output:
Route poster, pack
This keeps one space between words and none at the ends. There are more ways to remove spaces from a string.
Step 3: Remove Digits from a Python String Without Regex
translate() and isdigit() do the same job in plain Python. Save this as no_regex.py:
import string
item = "4821 Route66 poster, 2 pack 2026"
table = str.maketrans("", "", string.digits)
print(repr(item.translate(table)))
print(repr("".join(ch for ch in item if not ch.isdigit())))
Output:
' Route poster, pack '
' Route poster, pack '

The third argument of str.maketrans() lists the characters to delete. Here that’s 0 to 9.
The second line uses the isdigit() method. We’d pick it because it’s easier to read.
Step 4: Remove Whole Numbers but Keep Route66
Add \b, a word boundary, on both sides of the pattern. Save this as whole_numbers.py:
import re
item = "4821 Route66 poster, 2 pack 2026"
words = re.sub(r"\b\d+\b", "", item)
print(" ".join(words.split()))
Output:
Route66 poster, pack

A size like 24x36 stays too, because its digits touch a letter.
Step 5: Strip Numbers from the Start or End of a String
Pass string.digits to the strip methods. Save this as strip_numbers.py:
import string
item = "4821 Route66 poster, 2 pack 2026"
print(repr(item.lstrip(string.digits))) # start only
print(repr(item.rstrip(string.digits))) # end only
print(repr(item.strip(string.digits))) # both ends
Output:
' Route66 poster, 2 pack 2026'
'4821 Route66 poster, 2 pack '
' Route66 poster, 2 pack '

One space stays where the number was. Add another .strip() to remove it. The same methods also remove other characters from a string.
Step 6: Remove Prices, Decimals and Negative Numbers
\d+ matches digits only, so a price leaves its symbols behind. Save this as remove_prices.py:
import re
line = "Office chair $1,249.50 with -15.75 credit"
print(re.sub(r"\d+", "", line))
number = r"-?\$?\d+(?:,\d{3})*(?:\.\d+)?"
clean = re.sub(number, "", line)
print(" ".join(clean.split()))
Output:
Office chair $,. with -. credit
Office chair with credit

The longer pattern also takes the minus sign, dollar sign, commas and decimal part. Be careful with hyphens: it turns COVID-19 into COVID.
To keep the whole dollars, remove only the decimals.
Step 7: Remove Numbers from a Pandas Column
In pandas, use str.replace() with regex=True. We ran this on pandas 3.0.6. Save it as pandas_numbers.py:
import pandas as pd
df = pd.DataFrame({"item": ["4821 Route66 poster", "Desk lamp 2026"]})
clean = df["item"].str.replace(r"\b\d+\b", "", regex=True)
df["clean"] = clean.str.strip()
print(df)
Output:
item clean
0 4821 Route66 poster Route66 poster
1 Desk lamp 2026 Desk lamp
Without regex=True, pandas looks for the literal text of the pattern and changes nothing. For the opposite job, remove the non-numeric characters in pandas.
Which Python Method Removes Which Numbers?
| Remove | Use |
|---|---|
| Every digit | re.sub(r"\d+", "", s) |
| Every digit, no regex | s.translate(table) |
| Whole numbers only | re.sub(r"\b\d+\b", "", s) |
| Numbers at both ends | s.strip(string.digits) |
| Prices and decimals | The number pattern from Step 6 |
Mistakes When Removing Numbers from Strings
The common one is testing with isalpha(). Save this as isalpha_mistake.py to see why:
item = "4821 Route66 poster, 2 pack 2026"
print("".join(ch for ch in item if ch.isalpha()))
Output:
Routeposterpack
The spaces and the comma disappear too. Use not ch.isdigit() from Step 3 instead. If only the comma bothers you, remove the punctuation separately.
You may also run into these two:
- The string didn’t change. Save the result:
item = re.sub(...). - TypeError: expected string or bytes-like object, got ‘int’. Wrap the value in
str()first.
After You Remove the Numbers
Your Python text is now ready to compare, sort or group. Two more cleanup jobs:
Questions About Removing Numbers in Python
How do I remove all numbers from a string in Python?
Use re.sub(r"\d+", "", text) or translate().