Use the isdecimal()
method to check if a string only contains decimals, that is, only numbers from 0 to 9 and combinations of these numbers.
Subscript, superscript, roman numerals, and other variations will be returned as False
.
word = '32'
print(word.isdecimal())
#output: True
word = '954'
print(word.isdecimal())
#output: True
print("\u2083".isdecimal()) #unicode for subscript 3
#output: False
word = 'beach'
print(word.isdecimal())
#output: False
word = 'number32'
print(word.isdecimal())
#output: False
word = '1 2 3' #notice the space between chars
print(word.isdecimal())
#output: False
word = '@32$' #notice the special chars '@' and '$'
print(word.isdecimal())
#output: False
isdecimal()
is more strict than isdigit()
, which in its turn is more strict than isnumeric()
.