How to Remove Numbers From String in Python

Muhammad Waiz Khan Feb 02, 2024
  1. Remove Numbers From the String Using string.join() Method in Python
  2. Remove Numbers From the String in Python Using the string.translate() Method
  3. Remove Numbers From the String in Python Using the re.sub() Method
How to Remove Numbers From String in Python

This tutorial will look into various methods to remove the numbers or digits from the string in Python. We usually remove numbers from the data in the Natural Language Processing during the process of Data Cleaning.

Suppose we have a string abcd1234efg567, and we want to remove the digits from the string to get a string like abcdefg. We can remove the numbers from the string in Python using the following methods:

Remove Numbers From the String Using string.join() Method in Python

The string.join(iterable) method takes an iterable object iterable as input, joins its elements together using the value of the string as a separator, and returns the resulting string as an output.

To remove numbers from the string, we will first iterate through the string and select non-digit values, pass them to the string.join() method to join them and get the resulting string with non-digit characters as output.

The below example code demonstrates how to use the string.join() method to remove the numbers from the string in Python.

string = "abcd1234efg567"
newstring = "".join([i for i in string if not i.isdigit()])
print(newstring)

Output:

abcdefg

Remove Numbers From the String in Python Using the string.translate() Method

The string.translate(map) method in Python 2, takes a mapping table or dictionary as input and returns the string after replacing the specified characters with the characters defined in the input mapping table or dictionary.

The below example code demonstrates how to use the string.translate() method to remove the numbers from the string in Python 2.

from string import digits

string = "abcd1234efg567"
newstring = string.translate(None, digits)
print(newstring)

Output:

abcdefg

In Python 3, the string.translate(table) takes the translation table as input instead of the mapping table or dictionary, as in Python 2. Therefore, we need to use the str.maketrans() method to get a translation table to use it as an input for the string.translate() method.

The below example code demonstrates how to use the string.translate() and str.maketrans() methods to remove the numbers from the string in Python 3:

from string import digits

string = "abcd1234efg567"
table = str.maketrans("", "", digits)
newstring = string.translate(table)
print(newstring)

Output:

abcdefg

Remove Numbers From the String in Python Using the re.sub() Method

The re.sub(pattern, replace, string) takes the string as input and returns the string by replacing the non-overlapping occurrences of the pattern string (described as a regular expression) with the replace value in the string.

The regular expression for digits is [0-9]+. We just need to pass this as the pattern argument and '' as replace to remove the numbers from the input string using the re.sub() method.

The below example code demonstrates how to use the re.sub() method to remove numbers from the string:

import re

string = "abcd1234efg567"
newstring = re.sub(r"[0-9]+", "", string)
print(newstring)

Output:

abcdefg

Related Article - Python String