Use the built-in ord(char) function to get the integer Unicode (and ASCII) code point of a single-character string in Python—and use its inverse built-in chr(code_point) to convert an integer code point back into a single character.
Basic Example
# 1. ASCII characters (code points 0–127)
print(ord("A")) # Output: 65
print(ord("a")) # Output: 97
print(ord("0")) # Output: 48
print(ord("\n")) # Output: 10
# 2. Non-ASCII Unicode characters (accents, scripts, emojis)
print(ord("é")) # Output: 233 (U+00E9)
print(ord("€")) # Output: 8364 (U+20AC)
print(ord("🐍")) # Output: 128013 (U+1F40D)
# 3. Inverse operation: Convert integer code point back to a character with chr()
print(chr(65)) # Output: 'A'
print(chr(0x1F40D)) # Output: '🐍'
Key Details & Edge Cases
ord()strictly requires a string of length1(len(s) == 1):- Passing an empty string
ord("")or a multi-character stringord("ab")raisesTypeError: ord() expected a character, but string of length 2 found. - To convert an entire string into a list of code points, iterate over the string:
[ord(c) for c in "Hello"](returns[72, 101, 108, 108, 111]).
- Passing an empty string
- Multi-code-point grapheme clusters (emojis with skin tones, ZWJ sequences, or combining accents):
- Some visible glyphs consist of multiple Unicode code points—for example,
"e\u0301"(e+ combining acute accent,len == 2) or"👨👩👧"(len == 5). Callingord()directly on those raisesTypeError; inspect each underlying code point with[ord(c) for c in glyph]or normalize composed forms first viaunicodedata.normalize("NFC", glyph).
- Some visible glyphs consist of multiple Unicode code points—for example,
- Formatting code points in standard hexadecimal Unicode notation (
U+XXXX):ch = "€" print(f"U+{ord(ch):04X}") # Output: U+20AC - Code point (
ord()) vs. UTF-8 encoded byte values (str.encode()):- For ASCII characters (
0–127),ord("A")(65) equals the single UTF-8 byteb"A"[0](65). - For non-ASCII characters (
>= 128),ord("€")returns the Unicode scalar value (8364), whereas"€".encode("utf-8")returns the 3-byte UTF-8 sequenceb"\xe2\x82\xac"([226, 130, 172]). Useord()when you want the character’s code point, and.encode("utf-8")when you want its serialized wire/file bytes.
- For ASCII characters (
bytesindexing already yields integers (b"A"[0] == 65): If your input is already abytesobject (b"ABC"), indexingb[0]returns the integer byte value directly without callingord()(callingord(b"A")works for 1-bytebytes, butb[0]is faster and idiomatic).
Comparison of Character-to-Number Conversions in Python
| Operation / Goal | Syntax | Input Type | Output | Best Used When |
|---|---|---|---|---|
| Single character to Unicode/ASCII code point | ord("A") |
str (len == 1) |
int (0 to 0x10FFFF) |
Getting the numeric ASCII or Unicode scalar code point of a character. |
| Integer code point back to character | chr(65) |
int (0 to 1,114,111) |
str (len == 1) |
Constructing characters from decimal or hex (0x...) code points. |
| Entire string to list of code points | [ord(c) for c in s] |
str (any length) |
list[int] |
Inspecting every Unicode code point in a multi-character string. |
| String to raw UTF-8 byte integers | list(s.encode("utf-8")) |
str (any length) |
list[int] (0–255) |
Inspecting the serialized UTF-8 byte representation rather than code points. |
You must log in or register to comment.
