Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.combining()

character properties

Returns a character's canonical combining class as an integer: 0 for ordinary letters, non-zero for most diacritical marks.

Syntax

unicodedata.combining(chr, /)

The usual 'strip accents' recipe, NFD then dropping characters with combining(c) != 0, breaks Russian 'й': the breve is a mark too, so 'йод' becomes 'иод'. Zero does not guarantee a non-mark: U+034F COMBINING GRAPHEME JOINER has class 0 and category 'Mn'. To detect a mark, category(c).startswith('M') is more reliable.

Examples

import unicodedata
text = unicodedata.normalize('NFD', 'café, йод')
print(''.join(c for c in text if not unicodedata.combining(c)))  # → cafe, иод — й сломалась
print(unicodedata.combining('\u0301'), unicodedata.combining('\u034f'))  # → 230 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy