Tuesday 16 January 2018

python - How do I check if a string is a number (float)?

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.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...