ord() is a Python built-in function that converts one character into its integer ordinal value. For ASCII letters and digits, that value is the character's position in the ASCII table. section 18.1.2
Inspect characters in a word
>>> word = "Dont" >>> for char in word: ... print(char, ord(char)) ... D 68 o 111 n 110 t 116 section 18.1.2
Use ord() when a program needs numeric character values. In a gematria-style encoder, map ord() across the characters of a cleaned word and sum the results: `return str(sum(map(ord, re.sub('[^A-Za-z0-9]', '', word))))`. A list comprehension or loop expresses the same steps, while map() keeps this version concise. section 18.3.1
Keep these related behaviors in mind:
- Uppercase and lowercase characters have different ordinal values, such as `ord('D') == 68` and `ord('d') == 100`. section 18.1.2
- chr() reverses the operation by converting an integer back into a character, so `chr(68)` returns `'D'`. section 18.1.2
- Ordinal ranges expose contiguous ASCII groups such as `A-Z`, `a-z`, and `0-9`; `[A-Za-z0-9]` keeps those ranges separate in regular expressions. section 18.1.2
>>> ord('D')
68
>>> ord('o')
111
Use the compact REPL examples to verify ord() values directly, then compare them with chr() conversions. section 18.1.2
