What is the best possible way to check if a string can be represented as a number in Python?
The function I currently have right now is:
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float
in the main function is even worse.
I wanted to see which method is fastest. Overall the best and most consistent results were given by the
check_replace
function. The fastest results were given by thecheck_exception
function, but only if there was no exception fired - meaning its code is the most efficient, but the overhead of throwing an exception is quite large.Please note that checking for a successful cast is the only method which is accurate, for example, this works with
check_exception
but the other two test functions will return False for a valid float:Here is the benchmark code:
Here are the results with Python 2.7.10 on a 2017 MacBook Pro 13:
Here are the results with Python 3.6.5 on a 2017 MacBook Pro 13:
Here are the results with PyPy 2.7.13 on a 2017 MacBook Pro 13:
So to put it all together, checking for Nan, infinity and complex numbers (it would seem they are specified with j, not i, i.e. 1+2j) it results in:
You can use Unicode strings, they have a method to do just what you want:
Or:
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://docs.python.org/2/howto/unicode.html
I'd dispute both.
A regex or other string parsing would be uglier and slower.
I'm not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn't introduce much overhead because the most common exception is caught without an extensive search of stack frames.
The issue is that any numeric conversion function has two kinds of results
C (as an example) hacks around this a number of ways. Python lays it out clearly and explicitly.
I think your code for doing this is perfect.
In case you are looking for parsing (positive, unsigned) integers instead of floats, you can use the
isdigit()
function for string objects.String Methods -
isdigit()
There's also something on Unicode strings, which I'm not too familiar with Unicode - Is decimal/decimal