Reputation: 28313
In python strings have a method isnumeric
and isdigit
.
I was under the (wrong) impression that isnumeric
and isdecimal
would return True
for strings such as 123.45
and false otherwise. I'm running the following version of python:
Python 3.4.2 (default, Jan 7 2015, 11:54:58)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
And I found that isnumeric
and isdecimal
return true if all characters in the string are integers, but false if a '.'
(dot) is present. What causes this behaviour? Shouldn't '123.45'.isnumeric()
return True
?
>>> mystr_a = '123.45'
>>> mystr_b = '123'
>>>
>>> mystr_a.isnumeric()
False
>>> mystr_a.isdecimal()
False
>>> mystr_b.isnumeric()
True
>>> mystr_b.isdecimal()
Upvotes: 0
Views: 2802
Reputation: 6075
As defined in the Python documentation, isnumeric
returns True
if all characters within the string are numeric, otherwise False
. A dot is not considered to be numeric.
Upvotes: 2