all Python 3unicodedatafunction
unicodedata.lookup()
character names
Finds a character by its Unicode name, the inverse of name(); letter case in the name does not matter.
Syntax
unicodedata.lookup(name, /)
An unknown name raises KeyError, not ValueError as name() does, so you catch different things. It understands aliases and named sequences, so the result can have length 2: 'LATIN CAPITAL LETTER A WITH MACRON AND GRAVE' is two code points. If the name is known in advance, the literal '\N{SNOWMAN}' is simpler, and the compiler checks it.
Examples
import unicodedata
print(unicodedata.lookup('snowman') == '\N{SNOWMAN}') # → True
print(len(unicodedata.lookup('LATIN CAPITAL LETTER A WITH MACRON AND GRAVE'))) # → 2
unicodedata.lookup('NO SUCH CHAR') # → KeyError: "undefined character name 'NO SUCH CHAR'"
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy