Meaning
Returns the integer Unicode code point for a given single-character string. Solves the problem of needing a numeric representation of a character for arithmetic, comparison, or encoding work. Reached for when implementing character-level algorithms, custom hashing, or validating character ranges.
Primary Function
String/character encoding conversion
Communicative Purpose
Enables numeric inspection of individual characters by converting them to their Unicode code point integer values.
Pattern
ord(character)
Core Structure
ord(...)
Função primária
String/character encoding conversion
Propósito comunicativo
Enables numeric inspection of individual characters by converting them to their Unicode code point integer values.
Situações de gatilho
Text processing: converting characters to numeric codes for custom hash or checksum algorithms. Debugging: inspecting the exact code point of a non-ASCII character. Data validation: checking whether a character falls within a specific Unicode range such as ASCII.
Contextos
Python standard library, text‑processing scripts, encoding/decoding utilities, data validation
Padrão
ord(character)
Estrutura central
ord(...)
Slots de substituição
character: single-character string
Colocados típicos
- chr() (the inverse)
- bytes()
- string .encode()/.decode() methods
Substituições comuns
- Using f'{ord(c)}'
- bytes(c
- 'utf-8')[0] for UTF‑8 byte value
- or int.from_bytes(c.encode()
- 'big')
Erros comuns
Passing a string longer than one character (TypeError), passing an integer (TypeError), confusing ord with chr()
Similar / contraste
chr() converts code point to character; hex()/oct() give numeric representations; .encode() yields byte values for multi‑byte encodings
Interferências
Coming from C/C++: expecting a char‑to‑int cast works the same; in Python ord requires a one‑character string. Coming from JavaScript: confusing with charCodeAt() which also returns Unicode code point.
Família do chunk
- chr
- encode
- decode
- unicode handling
Nuance
ord works on Unicode code points; in narrow Python builds surrogate pairs are handled as two code points; it does not directly give byte values of UTF‑8 encoded characters.
Efeito pragmático
Makes character‑to‑code conversion explicit, useful for debugging and low‑level text processing.
Dica de memória
Think ‘ord’ as the ‘order’ of a character in the Unicode table.
Nota
In narrow Python builds (UTF-16), ord on characters outside the Basic Multilingual Plane returns a surrogate pair as two code points; in wide builds (UTF-32) it returns the actual Unicode code point.
Upgrade path
[ord(c) for c in s] to obtain a list of code points for an entire string
Log in to save chunks.