Tuesday 18 October 2016

Python: how do I use isnumeric()





Can someone kindly explain why the following throws an exception? And what should I do with variable s to find out whether it contains a number?



s = str(10)
if s.isnumeric():
print s


When I read Python documentation it seems to me the above should work. See:



https://docs.python.org/3/library/stdtypes.html?highlight=isnumeric#str.isnumeric




But what I get is:




"AttributeError: 'str' object has no attribute 'isnumeric'"




Any help is most appreciated.



Thank you!



Answer



replace isnumeric() with isdigit() as



s = str(10)
if s.isdigit():
print s

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...