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

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

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

Use the re.sub() method to remove all non-numeric characters except for dot . 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 = 'a3.1b4c' result = re.sub(r'[^0-9.]', '', my_str) print(result) # 👉️ '3.14'
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() #

To remove all non-numeric characters except for "." from a string:

  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'

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.

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.