Remove all Non-Numeric characters from a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
6 min

banner

# Table of Contents

  1. Remove all non-numeric characters from a String in Python
  2. Remove non-numeric characters except for "." in Python

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

Use the re.sub() method to remove all non-numeric characters from a string.

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 = 'bo_1bby_2_ha_3_dz.com' result = re.sub(r'[^0-9]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '123'

remove all non numeric characters from string

The code for this article is available on GitHub

If you need to remove all non-numeric characters except for the dot ".", click on the following 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 = '1bobby, 2hadz, 3com' result = re.sub(r'[^0-9]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ 123

using the re sub method

The code for this article is available on GitHub

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 = 'bo_1bby_2_ha_3_dz.com' result = re.sub(r'[^0-9]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '123'

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()

This is a three-step process:

  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 = 'bo_1bby_2_ha_3_dz.com' result = ''.join(char for char in my_str if char.isdigit()) print(result) # ๐Ÿ‘‰๏ธ '123'

remove all non numeric characters using join

The code for this article is available on GitHub

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 = 'bo_1bby_2_ha_3_dz.com' # ๐Ÿ‘‡๏ธ ['1', '2', '3'] 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 = 'bo_1bby_2_ha_3_dz.com' result = ''.join(char for char in my_str if char.isdigit()) print(result) # ๐Ÿ‘‰๏ธ '123'

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.

# Remove non-numeric characters except for "." in Python

If you need to remove the non-numeric characters except for ".", use the re.sub() method.

main.py
import re my_str = 'a3.1b4c' result = re.sub(r'[^0-9.]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '3.14'

remove non numeric characters except for dot

The code for this article is available on GitHub
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 except for dot 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.

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 or a dot.

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

main.py
import re my_str = 'a3.1b4c' result = re.sub(r'[^0-9.]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '3.14'

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

There is also a shorthand for the 0-9 range.

main.py
import re my_str = 'a3.1b4c' result = re.sub(r'[^\d.]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '3.14'

The \d character matches any Unicode decimal digit. This includes [0-9], and many other digit characters.

# Remove all non-numeric characters except "." from String using join()

This is a three-step process:

  1. Use a generator expression to iterate over the string.
  2. Check if each character is a digit or a dot and return the result.
  3. Use the str.join() method to join the characters that pass the test.
main.py
my_str = 'a3.1b4c' result = ''.join(char for char in my_str if char in '123456789.') print(result) # ๐Ÿ‘‰๏ธ '3.14'
The code for this article is available on GitHub

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 check if the current character is a digit or a dot and return the result.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

The generator object only contains the digits and dots from the string.

main.py
my_str = 'a3.1b4c' # ๐Ÿ‘‡๏ธ ['3', '.', '1', '4'] print(list(char for char in my_str if char in '123456789.'))

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

main.py
my_str = 'a3.1b4c' result = ''.join(char for char in my_str if char in '123456789.') print(result) # ๐Ÿ‘‰๏ธ '3.14'

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 and the dot without a separator.

# Remove all non-numeric characters from a string using a for loop

You can also use a for loop to remove the non-numeric characters from a string.

main.py
my_str = 'bo_1bby_2_ha_3_dz.com' result = '' for char in my_str: if char.isdigit(): result += char print(result) # ๐Ÿ‘‰๏ธ 123
The code for this article is available on GitHub

We declared a new variable and initialized it to an empty string.

On each iteration of the for loop, we check if the current character is a digit.

If the condition is met, we add the character to the result variable.

# Remove all non-numeric characters from string except dot using a for loop

The same approach can be used to remove all non-numeric characters from a string except for the dot ..

main.py
my_str = 'a3.1b4c' result = '' for char in my_str: if char.isdigit() or char == '.': result += char print(result) # ๐Ÿ‘‰๏ธ 3.14
The code for this article is available on GitHub

On each iteration, we check if the current character is a digit or a period.

If the condition is met, we add the character to the result variable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev