A program needs to inspect which character a text value represents.
What You Need to Understand
The built-in ord(character) returns the Unicode code point of a single character as an integer. It is useful when inspecting text protocols and understanding encodings.
When to Use It
A program needs to inspect which character a text value represents.
Step-by-Step Python Example
print(ord("A"))
print(ord("é"))
print(ord("😀"))
How the Example Works
Each call returns a Unicode code point, not a byte in UTF-8. For example A is 65, while é is 233.
Example Output
65
233
128512
A 65
B 66
C 67
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
for character in "ABC":
print(character, ord(character))
Important Behavior and Edge Cases
ord() requires exactly one Unicode character. A multi-character string raises TypeError; encoded bytes are a separate representation.
Try It Yourself
Inspect the code points of every character in the word Python.
Common Beginner Mistakes
ord()requires exactly one Unicode character. A multi-character string raisesTypeError; encoded bytes are a separate representation.- Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.
Key Takeaways
- The built-in
ord(character)returns the Unicode code point of a single character as an integer. - Each call returns a Unicode code point, not a byte in UTF-8.
ord()requires exactly one Unicode character.