all Python 3unicodedatafunction
unicodedata.east_asian_width()
character properties
Returns a character's East Asian width class: 'W' and 'F' take two terminal cells, 'Na' and 'H' one, 'A' is ambiguous.
Syntax
unicodedata.east_asian_width(chr, /)
len() counts code points, not screen width: '中文' is 2 characters and 4 cells, so ljust() alignment with CJK and emoji drifts. Cyrillic is 'A' (ambiguous): one cell in a Western terminal, two in a Chinese or Japanese one. A simple width estimate is 2 for 'W' and 'F' and 1 for everything else.
Examples
import unicodedata
print([unicodedata.east_asian_width(c) for c in 'a中й']) # → ['Na', 'W', 'A']
def width(text):
return sum(2 if unicodedata.east_asian_width(c) in 'WF' else 1 for c in text)
print(len('中文ab'), width('中文ab')) # → 4 6
See also
Open in the full glossarydocs.python.orgReport an inaccuracy