Python tip: name your unicode characters

Python tip: name your unicode characters

May 2, 2026

I got this tip from Trey Hunner’s newsletter. I would highly suggest signing up to his newsletter and looking at Python Morsels. If you use unicode in your code, create a variable instead of trying to use numerical codes:

>>> dashes = "-–﹘—"
>>> print(*dashes)
-   

Instead of this:

>>> flair = "\u2728"

Explicitly name Unicode characters

That’s why I prefer to use \N{...} to reference Unicode characters by their name:

>>> flair = "\N{sparkles}"

That makes it much easier for me to guess what that character actually represents:

>>> print(flair)

It works for all Unicode characters, including multi-word names:

>>> print("\N{waving hand sign}")
👋

How to find a character’s name

Don’t know the name of a Unicode character? Use unicodedata.name to look it up:

>>> import unicodedata
>>> for c in dashes:
...     print(f"{unicodedata.name(c)}: {c}")
...
HYPHEN-MINUS: -
EN DASH: 
SMALL EM DASH: 
EM DASH: 

You can also do a visual search on utf8.xyz (which Seth Larson started and I now own). Both searching by name (e.g. utf8.xyz/sparkles) or by the character itself (e.g. https://utf8.xyz/—) works.