Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python detect the symbol "²" as a digit?

Tags:

python

string

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?

like image 696
KianRST Avatar asked Nov 27 '20 17:11

KianRST


People also ask

Is Python a digit or a letter?

The Python isalpha() method returns true if a string only contains letters. Python isnumeric() returns true if all characters in a string are numbers. Python isalnum() only returns true if a string contains alphanumeric characters, without symbols.

How do you check if a symbol is a number in Python?

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values.

How do you check if a string has only digits in it python?

Check if String Contains Only Numbers using isdigit() method Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”.

How do you check if a string is a decimal number python?

Python string isdecimal() Method Python String isdecimal() function returns true if all characters in a string are decimal, else it returns False.


1 Answers

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.

like image 74
chepner Avatar answered Sep 28 '22 00:09

chepner