Can someone say if "²" is a symbol or a digit? (alt+1277, power of two)
print("²".isdigit())
# True
print("²".isnumeric())
# True
Because Python says it's a digit, but it's not actually a digit. Am I wrong? Or it's a bug?
It is explicitly documented as a digit:
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.
Regarding Numeric_Type, this is defined by Unicode:
Numeric_Type=Digit
Variants of positional decimal characters (Numeric_Type=Decimal) or sequences thereof. These include super/subscripts, enclosed, or decorated by the addition of characters such as parentheses, dots, or commas.
Python is smart enough to tag unicode characters as digits, just because it's possible.
To complete this good answer, note that you can even get the floating point representation of the character:
>>> from unicodedata import numeric
>>> numeric("²")
2.0
It's float because there are unicode versions of 1/2, 3/2 ...
int("08")
and as the base is specified (default, 10) it will convert to 8 all right
Nov 27, 2020 at 18:06
isdigit
norisnumeric
implies that a string consisting of these values can be used as an argument to, e.g.,int
. If you want to know if a strings
represents a particularint
value, don't inspects
, try it withint(s)
and catch an exception if raised.