Python: Unicode Escape Sequence

By Xah Lee. Date: . Last updated: .

Unicode Escape sequence lets you embed unicode character into string, by their Codepoint or character name. There are the following forms:

\u4_digits_hex
A character whose Unicode Codepoint is 4 hexadecimal digits or less. If less, must pad 0 in front.
import sys

print(sys.version)

# BLACK HEART SUIT, hexadecimal 2665
print("♥" == "\u2665")
\U8_digits_hex
A character whose Unicode Codepoint is more than 4 Hexadecimal digits. If less than 8 digits, you must pad 0 in front to make a total of 8 digits.
import sys

print(sys.version)

# GRINNING CAT FACE WITH SMILING EYES, hexadecimal 1f638
print("😸" == "\U0001f638")
\N{name}
Unicode character named name

(To find a Unicode Codepoint and name, see Unicode Search 😄)

import sys

print(sys.version)

# BLACK HEART SUIT, hexadecimal 2665
print("♥" == "\N{BLACK HEART SUIT}")

Python Unicode

Python String