Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.category()

character properties

Returns a character's general category as two letters: 'Lu' for an uppercase letter, 'Nd' for a decimal digit, 'Mn' for a combining mark and so on.

Syntax

unicodedata.category(chr, /)

The first letter is the class (L letters, N numbers, P punctuation, Z separators, M marks), so checks usually look at category(c)[0]. It is more precise than str methods: '²' is 'No' and int('²') fails though '²'.isdigit() is true; the no-break space is 'Zs'. Unassigned characters are 'Cn', and new emoji only become 'So' with a database version that knows them.

Examples

import unicodedata
print([unicodedata.category(c) for c in 'Aй5_ ²'])
# → ['Lu', 'Ll', 'Nd', 'Pc', 'Zs', 'No']
print('²'.isdigit(), unicodedata.category('²'))  # → True No

See also

Open in the full glossarydocs.python.orgReport an inaccuracy