Code Bytes · "How Computers Work" · Episode 2 · Bits and bytes
How text is stored
A computer stores text by giving every character its own number from a big table, today called Unicode, and storing that number in bits. In Python, ord() gives a character's number and chr() turns a number back into its character.
Letters out of ones and zeros
Last episode you saw that a computer stores everything as bits: ones and zeros. So how does it store the letter A?
The trick is simple: every character gets its own number. The computer uses a big table that pairs each character with a number. To store a letter, it stores that letter's number. To show a letter, it looks the number up in the table.
What counts as a character
A character is one single letter, digit, space or symbol. A is a character. So are 7, a space and !. Each one has its own number in the table, even the space.
ord(): from a character to its number
Python's built-in ord() function gives you the number of a one-character string:
``python print(ord("A")) ``
`` 65 ``
So capital A's number in the table is 65. ("ord" is short for ordinal, a word for a position number.)
chr(): from a number back to its character
chr() goes the other way: give it a number and it hands back the character. ("chr" is said "char", short for character.)
``python print(chr(66)) ``
`` B ``
Capital and small letters are different characters
Small letters have their own, different numbers:
``python print(ord("a")) ``
`` 97 ``
Capital A is 65 but small a is 97. To a computer they are different characters. That's why, in code, "Hello" == "hello" is False: the first letters have different numbers.
Stored as bits
The numbers are what actually get saved, as bits. Python's bin() (from the last episode) shows 65 in binary:
``python print(bin(65)) ``
`` 0b1000001 ``
Padded to a full byte, that's 01000001, and that byte is exactly how capital A is stored in a plain text file.
Unicode: today's table
Today's standard table is called Unicode. It gives a number to the characters of almost every language in the world, from Latin and Greek letters to Arabic, Hindi, Chinese and Korean, plus symbols and emoji. Its first 128 numbers match an older table called ASCII, which is why A is 65 in both.
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python print(ord("!")) print(chr(98)) ``
You should see:
`` 33 b ``
The ! symbol is number 33 in the table, and 98 is small b (one after small a, which is 97). Try ord() on the first letter of your name, then chr() on the number you get back.
Good to know
- Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown.
ord()only takes one character.ord("AB")gives an error.- Even a space is a character:
print(ord(" "))prints32. The digit7as text is a character too:print(ord("7"))prints55, not 7. - "Stored as bits" is simplified. The number (Unicode calls it a code point) is turned into bytes by an encoding, usually UTF-8. UTF-8 stores A as the single byte
01000001and uses two to four bytes for most other characters.print(list("Hi".encode()))prints[72, 105]: the bytes for H and i. - Capital letters A to Z are numbers 65 to 90, and small letters a to z are 97 to 122, both in alphabet order.
- Network Bytes EP19 goes deeper on counting in binary.
Answer to the comment question
What does print(ord("C")) print?
67
The letters are in alphabet order in the table: capital A is 65, B is 66, so C is 67. Running it in Python 3 prints:
`` 67 ``
Test your knowledge
Quick quiz: How text is stored
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "Built-in Functions": ord(): returns the Unicode code point of a one-character string, for example ord("a") is 97
- Python Software Foundation, "Built-in Functions": chr(): returns the character whose Unicode code point is the given integer
- Python Software Foundation, "Unicode HOWTO": characters are stored as code points and turned into bytes by an encoding such as UTF-8
- The Unicode Consortium, "What is Unicode?": one number for every character, in every modern language, including emoji
Video transcript
If a computer only stores ones and zeros, how does it store a letter?
Every letter you type is secretly a number. The computer uses a big table that pairs each character with its own number.
A character is one single letter, digit, space or symbol.
In Python, ord of capital A gives sixty-five. That's capital A's number in the table.
Chr goes the other way. Chr of sixty-six gives capital B.
Small letters get different numbers: small a is ninety-seven. To a computer, capital A and small a are different characters.
Those numbers are stored as bits. Today's standard table is called Unicode, and it covers almost every language, plus emoji.
What does this print? Comment your answer below!
That's your byte.
More from "How Computers Work"
- "How Computers Work" EP4RAM vs storage
- "How Computers Work" EP5The fetch-decode-execute cycle
- "How Computers Work" EP6What an operating system does
- "How Computers Work" EP7Machine code vs high-level code