ord()

Turn a single character into its Unicode codepoint — the inverse of chr.

Built-in functionPython 1.0+Live demo
Common call
ord("A") # 65
Returns
an int
Replaces
the "what codepoint is this character?" lookup
Watch out
input must be exactly one character — not empty, not multiple
ord(ccA string of length exactly 1 (one Unicode character). Empty strings and multi-character strings both raise TypeError.type: str · required)
int

Demo

Live evaluation
Try:
Inputs
cstrone character
Output
ord('A')
65

ord takes a string of length exactly 1 and returns its Unicode codepoint as an integer. Empty strings and multi-character strings both raise TypeError. chr is the inverse operation.

Parameters

NameTypeRequiredDescription
cstryesA string of length exactly 1 (one Unicode character). Empty strings and multi-character strings both raise TypeError.

Return value

intThe Unicode codepoint of the single character c, as an integer in 0..0x10FFFF.

Common patterns

Letter to index (a=0)
Subtract from ord("a") for a zero-based letter index.
idx = ord(ch.lower()) - ord("a")
Digit character to int
Fast alternative to int(ch) for single-digit characters.
digit = ord(ch) - ord("0")
Case-only comparison
ord() gives you the numeric distance between letters, useful in cipher math.
shift = ord("A") - ord("a")   # -32

Examples

1. Uppercase
ord("A")
Returns
65
2. Lowercase
ord("a")
Returns
97
3. Digit
ord("0")
Returns
48
4. Greek letter
ord("Ω")
Returns
937
5. Emoji
ord("😀")
Returns
128512
6. Empty raises
ord("")
Returns
TypeError: ord() expected a character, but string of length 0 found
7. Two chars raise
ord("AB")
Returns
TypeError: ord() expected a character, but string of length 2 found

Pitfalls

1. Input must be EXACTLY one character
Empty strings and multi-character strings both raise TypeError. There is no default and no silent truncation.
Wrong length
ord("")
ord("AB")
TypeError: ord() expected a character, but string of length 0/2 found
One at a time
for ch in "AB":
    print(ord(ch))
65 66
2. Grapheme clusters can be more than one "character"
A visible glyph may consist of multiple codepoints (e.g., emoji + skin-tone modifier, or a letter + combining accent). Python counts codepoints, not graphemes — such strings have len > 1 and ord() rejects them.
Length > 1
ord("👨🏽")   # emoji + skin tone
TypeError: ord() expected a character, but string of length 2 found
Iterate codepoints
[ord(c) for c in "👨🏽"]
[128104, 127997]
3. Bytes and str give different codepoints? No — bytes give byte VALUES
ord() on a bytes object of length 1 returns the byte value (0..255), not a Unicode codepoint. Same range as str for ASCII; different for anything higher.
Confusing
ord(b"A")     # bytes
ord("A")      # str
65 65 # match here — but only for ASCII
Know the input type
ord("Ω")      # 937 — str codepoint
ord(b"\xce"[0:1])  # 206 — one raw byte
different worlds

When to use

Use it
  • Turning letters into indexes or offsets
  • Case and cipher math without table lookups
  • Building parsers where character codepoints matter
  • Debugging Unicode issues by inspecting exact codepoints
Reach for something else
  • Strings with more than one character → iterate first
  • Grapheme-aware handling of emoji + modifiers → use a Unicode library
  • Bytes when you want codepoints (or vice versa) → know your input

Notes

Complexity
O(1)
Return
int in 0..0x10FFFF for str input; 0..255 for bytes/bytearray input
CPython impl
Python/bltinmodule.c :: builtin_ord
Memory
No allocation
Thread-safe
Yes — a pure computation

FAQ

ord expects one character, not a string. To get all codepoints of a string, use a list comprehension: `[ord(c) for c in s]`.

History

1.0
ord() has been a builtin since Python 1.0.
3.0
Full Unicode support — returns codepoints up to 0x10FFFF for str input.