How to Remove Numbers from Strings in Python?

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 Smth

I have executed the above example code and added the screenshot below.

Remove Numbers from Strings in Python

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  William

This 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.

How to Remove Numbers from Strings in Python

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:

Chicago

I have executed the above example code and added the screenshot below.

Remove Numbers from Strings in Python List Comprehension

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:

StatuefLiberty

Here, 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.