How Can I Verify If A String Is A Valid Float?
What I want to do is to verify if a string is numeric -- a float -- but I haven't found a string property to do this. Maybe there isn't one. I have a problem with this code: N = r
Solution 1:
try:
N = float(N)
except ValueError:
pass
except TypeError:
pass
This tries to convert N
to a float
. However, if it isn't possible (because it isn't a number), it will pass
(do nothing).
I suggest you read about try
and except
blocks.
You could also do:
try:
N = float(N)
except (ValueError, TypeError):
pass
Post a Comment for "How Can I Verify If A String Is A Valid Float?"