Remove the Numbers from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the Numbers from a String in Python #

To remove the numbers from a string:

  1. Use a generator expression to iterate over the string.
  2. Use the isdigit() method to filter out all digits.
  3. Use the str.join() method to join the remaining characters into a string.
main.py
my_str = '12apple 345banana 678kiwi' # ✅ remove numbers from a string result = ''.join(char for char in my_str if not char.isdigit()) print(result) # 👉️ 'apple banana kiwi'
For a regex solution, scroll down to the next subheading.

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.isdigit() method to check if the current character is not a digit and return the result.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

Notice that the test for if the character is a digit is negated with the not operator.

main.py
my_str = '12apple 345banana 678kiwi' # ✅ remove numbers from a string result = ''.join(char for char in my_str if not char.isdigit()) print(result) # 👉️ 'apple banana kiwi'

We only want to keep non-digit characters in the generator object.

The last step is to use the str.join() method to join the non-digit characters into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

main.py
print(''.join(['a', 'b', 'c', 'd'])) # 👉️ 'abcd'

The string the method is called on is used as the separator between the elements.

For our purposes, we joined the characters with an empty string separator.

Remove the Numbers from a String using Regex #

Use the re.sub() method to remove the numbers from a string, e.g. result = re.sub(r'\d+', '', my_str). The re.sub() method will remove the numbers from the string by replacing each number with an empty string.

main.py
import re my_str = '12apple 345banana 678kiwi' result = re.sub(r'\d+', '', my_str) print(result) # 👉️ 'apple banana kiwi' print(''.join(['a', 'b', 'c', 'd'])) # 👉️ 'abcd'

We used the re.sub() method to remove the numbers from a string.

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

The first argument we passed to the method is a regular expression.

The \d character matches the digits [0-9] (and many other digit characters).

The plus + matches the preceding character (any digit) 1 or more times.

The second argument we passed to the re.sub method is the replacement string.

We want to remove all numbers from the string, so we used an empty string as the replacement.

If the string doesn't contain any numbers, the re.sub method will return the original string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.