In python.
If I have a string, how can I check if the string is a number or not?
string1 = "1"
(True) or string2 = "h"
(False) string3 = "2g"
(False).
I tried to check if it is a letter but it doesn't work, the string also can be "," "." or " " and then it won't work.
Asked
Active
Viewed 102 times
-1

David Walschots
- 12,279
- 5
- 36
- 59

אופיר אורן
- 1
- 3
-
https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float and https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except – cs95 Mar 19 '18 at 14:48
1 Answers
0
Using isdigit() method:
>>> a = "0123"
>>> a.isdigit()
True
>>> b = "012a"
>>> b.isdigit()
False

marcdecline
- 186
- 4
- 22