🕯️ Magic Note
Unicode assigns a unique integer to every character across all human languages. ord() reveals this integer. Its counterpart, chr(), does the reverse. It takes an integer and returns the corresponding character. Together, they form a bridge between text and numbers.
- Works on any single character, including emojis and non-English scripts
- Raises TypeError if given a string longer than one character
- The reverse function is chr() turning numbers back into characters
- Useful for encryption, sorting, or character manipulation
For example, checking if a character is uppercase by comparing its code point with ord(“A”) and ord(“Z”).
For lowercase, compare with ord(“a”) and ord(“z”). This works across different locales.
| Character | ord() Value |
|---|---|
| “A” | 65 |
| “a” | 97 |
| “0” | 48 |
| ” ” (space) | 32 |
| “🙂” | 128578 |
| “é” | 233 |
For converting longer strings to code points, use a loop or map(ord, string). Also be aware that some emojis are actually made of multiple code points (like skin tone modifiers), and ord() only reads the first one.
Python
# Converting characters to their code points
char = “A”
code = ord(char)
print(f”The hidden number of {char} is {code}”)
# Output: The hidden number of A is 65
Python
# Checking if a character is uppercase
def is_uppercase(char):
return ord(“A”) <= ord(char) <= ord("Z")
print(is_uppercase(“M”))
# Output: True
print(is_uppercase(“z”))
# Output: False
Python
# Converting an entire string to code points
text = “ABC”
codes = [ord(c) for c in text]
print(codes)
# Output: [65, 66, 67]
# Reverse using chr()
original = “”.join(chr(code) for code in codes)
print(original)
# Output: ABC
- Passing a multi-character string to ord(), causing a TypeError
- Confusing ord() with chr(), trying to get a character from a number using the wrong function
- Assuming ord() works with ASCII only, it supports the full Unicode range including emojis and non-English scripts
⚡ Whisper
Every letter wears a mask. Behind the shape is a number, silent and hidden. ord removes the mask and reveals the truth. The character becomes data. The symbol becomes a key.