Removing all non-numeric characters from string in Python

43

Removing all non-numeric characters from string in Python -

# Many right answers but in case you want it in a float, directly, without using regex:

x= '$123.45M'
float(''.join(c for c in x if (c.isdigit() or c =='.'))


#You can change the point for a comma depending on your needs.
#change for this if you know your number is an integer

x='$1123'    
int(''.join(c for c in x if c.isdigit())

Comments

Submit
0 Comments