简体   繁体   中英

Why does Python detect the symbol “²” as a digit?

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...

(see How to convert unicode numbers to ints? )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM