Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.decimal()

numeric values

Returns the value of a decimal digit (category Nd) as an int; it works for Arabic-Indic, Devanagari and other digits too.

Syntax

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

It is the narrowest of the three functions: '²' and '½' are not digits for it, so it raises ValueError unless a default is given. Its set matches str.isdecimal() and what int() accepts: int('٣') == 3, while int('²') fails. So validate input before int() with isdecimal(), not isdigit().

Examples

import unicodedata
print(unicodedata.decimal('٣'), int('٣'))  # → 3 3
print(unicodedata.decimal('²', -1), '²'.isdigit())  # → -1 True
unicodedata.decimal('²')  # → ValueError: not a decimal

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy