ord() is a built-in Python function that returns the Unicode code point (integer) of a single character — the inverse of chr(). It works on any Unicode character (ASCII, accented letters, emojis, symbols) and is essential for character-to-number conversion, encoding analysis, sorting custom orders, and text processing. In 2026, ord() remains a core tool in data science (NLP tokenization, feature extraction from text), software engineering (Unicode debugging, custom collation), and algorithms (anagram detection, cipher implementation) — fast, reliable, and supports the full Unicode range (0 to 1114111).
Here’s a complete, practical guide to using ord() in Python: basic conversion, Unicode ranges, real-world patterns (earthquake place name analysis, custom sorting, emoji detection), and modern best practices with type hints, error handling, performance, and integration with NumPy/Dask/Polars/pandas/strings.
Basic ord() usage — single character to Unicode integer.
print(ord('A')) # 65 (ASCII uppercase A)
print(ord('a')) # 97 (ASCII lowercase a)
print(ord('€')) # 8364 (Euro sign)
print(ord('?')) # 128512 (grinning face emoji)
print(ord('?')) # 127757 (globe showing Europe-Africa)
print(ord('?')) # 9733 (black star)
Unicode ranges & limits — what ord() supports.
# ASCII range (0–127)
print(ord('A'), ord('Z'), ord('0'), ord('9')) # 65 90 48 57
# Extended Latin & accented characters
print(ord('é'), ord('ç'), ord('ñ')) # 233 231 241
# Emoji & symbols (supplementary planes)
print(ord('?'), ord('?'), ord('??')) # 128522 128640 10084
# Invalid input raises TypeError
try:
ord('ab') # must be single character
except TypeError as e:
print(e) # ord() expected a character, but string of length 2 found
Real-world pattern: earthquake place name analysis — ord() for character-level features & custom sorting.
import pandas as pd
df = pd.DataFrame({
'place': ['Héllo, California', 'São Paulo, Brazil', '??, Japan', 'München, Germany']
})
# Character-level features: average ord() value as "complexity" proxy
df['avg_ord'] = df['place'].apply(lambda s: sum(ord(c) for c in s) / len(s))
print(df.sort_values('avg_ord')) # higher avg_ord = more non-ASCII chars
# Custom sort key: lexicographic with accent-aware ordering
def accent_weight(s):
return sum(ord(c) for c in s) # simple proxy for accent weight
df_sorted = df.sort_values('place', key=lambda col: col.map(accent_weight))
print(df_sorted['place'].tolist())
# ['Héllo, California', 'München, Germany', 'São Paulo, Brazil', '??, Japan']
# Emoji detection in place names (rare but possible)
df['has_emoji'] = df['place'].apply(lambda s: any(ord(c) > 127 for c in s))
print(df[df['has_emoji']])
Best practices for ord() in Python & data workflows. Use ord(c) — for single-character Unicode code point (must be length 1 string). Modern tip: use Polars pl.col('text').str.to_codepoints() — fast vectorized ord() over strings; NumPy/Dask for array-based code points. Prefer ord() over manual ord lookup — clearer and portable. Validate input — catch TypeError for multi-char or non-string. Add type hints — def char_to_code(c: str) -> int: return ord(c). Use in sorting — sorted(lst, key=lambda x: ord(x[0])) for first-char order. Use ord() in hashing — sum(ord(c) for c in s) for simple string hash. Use ord() with chr() — round-trip: chr(ord('A')) == 'A'. Use unicodedata — for name/category: unicodedata.name(chr(8364)) ? 'EURO SIGN'. Use ord() in regex — re.compile(f'[\\x{ord("A"):x}-\\x{ord("Z"):x}]'). Use ord() in compression — frequency analysis. Use ord() in encryption — Caesar cipher: chr((ord(c) - 65 + shift) % 26 + 65). Profile performance — ord() is fast; vectorize with NumPy/Polars for large text. Use ord() with surrogate pairs — ord('?') = 128512 (Python handles correctly). Use ord() in terminal UI — ANSI escape codes. Use ord() in font rendering — glyph mapping.
ord(c) returns the Unicode code point integer of a single character — works on ASCII, accented letters, emojis, symbols, and the full Unicode range. In 2026, use for character encoding, custom sorting, feature extraction, and integrate with Polars/NumPy/Dask for vectorized code point operations. Master ord(), and you’ll handle text-to-number conversion cleanly and efficiently in any string or Unicode workflow.
Next time you need the numeric code of a character — use ord(). It’s Python’s cleanest way to say: “Give me the Unicode integer for this character — any Unicode char, instantly.”