The following two expressions return True
'2'.isdigit()
chr(178).isdigit()
the later is exponent.
I am looking for a way to distinguish them for all numbers
This is as documented.
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
If you want to check if python is able to parse a string as a number, an idiomatic approach is to use try-except.
def is_really_digit(s):
try:
int(s)
return True
except ValueError:
return False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With