How do you remove non numeric values in python?

Fastest approach, if you need to perform more than just one or two such removal operations (or even just one, but on a very long string!-), is to rely on the translate method of strings, even though it does need some prep:

>>> import string
>>> allchars = ''.join(chr(i) for i in xrange(256))
>>> identity = string.maketrans('', '')
>>> nondigits = allchars.translate(identity, string.digits)
>>> s = 'abc123def456'
>>> s.translate(identity, nondigits)
'123456'

The translate method is different, and maybe a tad simpler simpler to use, on Unicode strings than it is on byte strings, btw:

>>> unondig = dict.fromkeys(xrange(65536))
>>> for x in string.digits: del unondig[ord(x)]
... 
>>> s = u'abc123def456'
>>> s.translate(unondig)
u'123456'

You might want to use a mapping class rather than an actual dict, especially if your Unicode string may potentially contain characters with very high ord values (that would make the dict excessively large;-). For example:

>>> class keeponly(object):
...   def __init__(self, keep): 
...     self.keep = set(ord(c) for c in keep)
...   def __getitem__(self, key):
...     if key in self.keep:
...       return key
...     return None
... 
>>> s.translate(keeponly(string.digits))
u'123456'
>>> 

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.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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.

How do I remove non numeric values from a list 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.]

How do you remove non digits from a string in Python?

The syntax using this method in python 3 is x. translate(str. maketrans('', '', string. digits)) and x..
so [a-z] means all lowercase letters or for uppercase we have to [A-Z] ? ... .
[a-z] will work for both lower and uppercases :).

How do I remove non numeric characters from a string?

In order to remove all non-numeric characters from a string, replace() function is used. replace() Function: This function searches a string for a specific value, or a RegExp, and returns a new string where the replacement is done.

How do you remove digits in Python?

In Python, an inbuilt function sub() is present in the regex module to delete numbers from the Python string. The sub() method replaces all the existences of the given order in the string using a replacement string.