Removing non numeric characters from a string Removing non numeric characters from a string python python

Removing non numeric characters from a string


The easiest way is with a regexp

import rea = 'lkdfhisoe78347834 (())&/&745  'result = re.sub('[^0-9]','', a)print result>>> '78347834745'


Loop over your string, char by char and only include digits:

new_string = ''.join(ch for ch in your_string if ch.isdigit())

Or use a regex on your string (if at some point you wanted to treat non-contiguous groups separately)...

import res = 'sd67637 8' new_string = ''.join(re.findall(r'\d+', s))# 676378

Then just print them out:

print(old_string, '=', new_string)


There is a builtin for this.

string.translate(s, table[, deletechars])

Delete all characters from sthat are in deletechars (if present), and then translate thecharacters using table, which must be a 256-character string givingthe translation for each character value, indexed by its ordinal. Iftable is None, then only the character deletion step is performed.

>>> import string>>> non_numeric_chars = ''.join(set(string.printable) - set(string.digits))>>> non_numeric_chars = string.printable[10:]  # more effective method. (choose one)'sd67637 8'.translate(None, non_numeric_chars)'676378'

Or you could do it with no imports (but there is no reason for this):

>>> chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'>>> 'sd67637 8'.translate(None, chars)'676378'