ASCII & Character Encoding in Python

Understanding ord() and chr()

Python Programming Course

What is ASCII?

Computers don’t “understand” letters. They only understand numbers (specifically, binary).

  • ASCII stands for American Standard Code for Information Interchange.
  • It acts as a “lookup table” that maps specific characters to specific numbers.
  • The original standard uses 7 bits, representing 128 characters (0-127).

How the Table is Structured

The ASCII table is divided into three main sections:

  1. Control Characters (0–31): Non-printable codes like “Backspace” or “New Line.”
  2. Printable Characters (32–126): Letters, digits, and punctuation.
  3. Delete (127): A special control character.

Key Character Ranges

Knowing these ranges helps when writing logic for data validation or “shifting” ciphers:

Range Character Type
48–57 Digits (0–9)
65–90 Uppercase (A–Z)
97–122 Lowercase (a–z)
32 Space

Pro Tip: Note that ‘A’ (65) and ‘a’ (97) are exactly 32 apart. This is why bitwise operations can easily toggle case!

The ord() Function

The ord() function (short for ordinal) takes a single character and returns its integer ASCII/Unicode value.

# Converting characters to integers
print(ord('A'))  # Output: 65
print(ord('a'))  # Output: 97
print(ord('$'))  # Output: 36

The chr() Function

The chr() function is the inverse. It takes an integer and returns the corresponding character.

# Converting integers to characters
print(chr(65))   # Output: 'A'
print(chr(100))  # Output: 'd'
print(chr(8364)) # Output: '€' (Python uses Unicode!)

The Relationship

Think of them as two sides of the same coin. For any character c:

\[chr(ord(c)) = c\]

And for any valid integer i:

\[ord(chr(i)) = i\]

Beyond ASCII: Unicode

While ASCII stops at 127, modern Python uses Unicode (UTF-8).

  • The first 128 characters of Unicode are identical to ASCII.
  • Unicode allows for over 1.1 million characters, including Emojis and diverse languages.
print(ord('🐍')) # Output: 128013
print(chr(128013)) # Output: 🐍

Summary Checklist

  • Computers map characters to numbers using ASCII.
  • ord(char) \(\rightarrow\) Integer.
  • chr(int) \(\rightarrow\) Character.
  • Use ranges (65-90, 97-122) for text processing logic.