>>> a = "12345"
>>> int(a)
12345
>>> b = "12345G"
>>> int(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12345G'
You can do it:
def isNumber(s):
try:
int(s)
except ValueError:
return False
return True
If you want a floating point number, replace intwith float(thanks to @cobbal).
source
share