Remove all non-numeric characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove all non-numeric characters from a String in Python #

Use the re.sub() method to remove all non-numeric characters from a string, e.g. result = re.sub(r'[^0-9]', '', my_str). The re.sub() method will remove all non-numeric characters from the string by replacing them with empty strings.

main.py
import re my_str = 'a1s2d3f4g5' result = re.sub(r'[^0-9]', '', my_str) print(result) # 👉️ '12345'
If you're looking to avoid using regular expressions, scroll down to the next subheading.

We used the re.sub() method to remove all non-numeric characters 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.

main.py
import re my_str = '1apple, 2apple, 3banana' result = re.sub(r'[^0-9]', '', my_str) print(result) # 👉️ 123

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub() method is a regular expression.

The square brackets [] are used to indicate a set of characters.

If the first character of the set is a caret ^, all characters that are not in the set will be matched.

In other words, our set matches any character that is not a digit in the range 0-9.

The second argument we passed to the re.sub() method is the replacement for each match.

main.py
import re my_str = 'a1s2d3f4g5' result = re.sub(r'[^0-9]', '', my_str) print(result) # 👉️ '12345'

We want to remove all non-numeric characters, so we replace each with an empty string.

There is also a shorthand for the [^0-9] character set.

main.py
import re my_str = 'a1s2d3f4g5' result = re.sub(r'\D', '', my_str) print(result) # 👉️ '12345'

The \D special character matches any character that is not a digit. It is very similar to the [^0-9] character set but includes more digit characters.

Remove all non-numeric characters from a String using join() #

To remove all non-numeric characters from a string:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isdigit() character to check if each character is a digit.
  3. Use the str.join() method to join the digits into a string.
main.py
my_str = 'a1s2d3f4g5' result = ''.join(char for char in my_str if char.isdigit()) print(result) # 👉️ '12345'

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 a digit and return the result.

The generator object only contains the digits from the string.

main.py
my_str = 'a1s2d3f4g5' # 👇️ ['1', '2', '3', '4', '5'] print(list(char for char in my_str if char.isdigit()))

The last step is to join the digits into a string.

main.py
my_str = 'a1s2d3f4g5' result = ''.join(char for char in my_str if char.isdigit()) print(result) # 👉️ '12345'

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

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

For our purposes, we called the join() method on an empty string to join the digits without a separator.

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.