Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.digit()

numeric values

Returns the value of a digit character as an int, including superscript and circled digits such as '²' and '①'.

Syntax

unicodedata.digit(chr, default=None, /)

It is wider than decimal(): '²' is 2 here, which is exactly the set of str.isdigit(). But int() rejects such characters, so an isdigit() check before int() lets '²' through and the program crashes. Fractions such as '½' and Roman numerals are not digits (ValueError); only numeric() understands them.

Examples

import unicodedata
print(unicodedata.digit('²'), unicodedata.digit('①'))  # → 2 1
try:
    int('²')
except ValueError as e:
    print(e)  # → invalid literal for int() with base 10: '²'
unicodedata.digit('½')  # → ValueError: not a digit

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy