In this tutorial, I will explain how to remove numbers from strings in Python. As a data scientist working with US census data, I frequently encounter strings containing text and numeric digits. To clean and process this data effectively I explored various ways, Let us see some important methods to achieve this task.
Remove Numbers from Strings in Python
Python provides several ways to remove numeric digits from a given string, which we’ll cover in detail with examples.
Method 1: Use re.sub() Function
The re.sub() function from the re module is used to substitute parts of a string that match a given regular expression pattern with a specified replacement.
import re
name = "John123 Sm4th"
cleaned_name = re.sub(r'\d+', '', name)
print(cleaned_name) Output:
John SmthI have executed the above example code and added the screenshot below.

In this code, we use the re.sub() function to replace all occurrences of numeric digits with an empty string. The regular expression pattern \d+ matches one or more consecutive digits. By passing an empty string as the replacement, we effectively remove the numbers from the string.
Handle Special Cases
Sometimes, you might want to remove numbers only in specific contexts. For example, let’s say you have US phone numbers mixed with names, like this:
data = "Emma (123) 456-7890 William"To remove the phone number while keeping the names intact, you can use a more targeted regular expression:
import re
cleaned_data = re.sub(r'\(\d{3}\)\s*\d{3}-\d{4}', '', data)
print(cleaned_data) Output:
Emma WilliamThis pattern matches a specific format: three digits enclosed in parentheses, followed by an optional space, and then three digits, a hyphen, and four more digits. By replacing this pattern with an empty string, we remove the phone number from the string.
Read Difference Between Functions and Methods in Python
Method 2: Use str.isalpha() Function
This method checks if each character in a string is alphabetic using the isalpha() method. It’s useful for manually filtering out non-alphabetic characters.
name = "George W. B3sh"
cleaned_name = ""
for char in name:
if char.isalpha() or char == " ":
cleaned_name += char
print(cleaned_name)Output:
"George W Bsh"I have executed the above example code and added the screenshot below.

Here, isalpha() ensures that only alphabetic characters are retained. Spaces are manually preserved.
Check out How to Square a Number in Python?
Method 3: Use List Comprehension
This is a concise way to achieve the same result as the loop. It combines filtering and iteration in a single line.
city_name = "Ch1cago"
char_mapping = {'1': 'i'} # Define a mapping of digits to letters
cleaned_name = "".join([char_mapping[char] if char in char_mapping else char for char in city_name])
print(cleaned_name)Output:
ChicagoI have executed the above example code and added the screenshot below.

In this example, the list comprehension filters out all non-alphabetic characters and joins the remaining characters into a cleaned string.
Read How to Check if a Number is NaN in Python?
Method 4: Use filter() Function
The filter() function allows you to apply a filtering condition to each character in the string, keeping only those that satisfy the condition.
landmark = "Statue0fLiberty"
cleaned_landmark = "".join(filter(str.isalpha, landmark))
print(cleaned_landmark)Output:
StatuefLibertyHere, filter() applies the str.isalpha method to each character in the string, retaining only alphabetic characters.
Check out How to Generate Random Numbers Between 0 and 1 in Python?
Conclusion
In this tutorial, I explained to you how to remove numbers from strings in Python. I covered important methods to achieve this task, using the re.sub() function, using str.isalpha() function, using list comprehension and by using filter() function.
You may also like to read:
- How to Convert Decimal Numbers to Binary in Python?
- How to Round Numbers to 2 Decimal Places in Python?
- How to Generate Credit Card Numbers in Python for Testing?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.