How does a computer store a letter?
Your friend sends you a message. It says: Café, 日本, 😀. (日本 is Japan in Japanese.)
A computer only stores numbers. So how does it store the letter C? And how does it store a Japanese word or a smiling face?
The answer: every character has its own number. Today you find out which numbers.
Key words
| Word | What it means | Example |
|---|---|---|
| Character | One letter, digit, symbol or emoji. | A, 7, ?, 😀 |
| Character code | The number a computer uses for a character. | A is 65 |
| ASCII | An old, small set of 128 character codes. It covers English letters, digits and symbols. | A is 65 |
| UTF-8 | A modern way to store every character in every language. | é, 日, 😀 |
| Byte | A unit of storage. Files are measured in bytes. | A short word takes a few bytes |
| Encoding | The way text is turned into codes so it can be saved or sent. | ASCII or UTF-8 |
The cells students fill in are left blank in this preview.
Some ASCII codes
| Character | Code | Note |
|---|---|---|
| A | 65 | Capital letters start at 65 |
| B | 66 | The next letter is the next number |
| a | 97 | Small letters start at 97 |
| 0 | 48 | The character 0. It is a character, not the number zero. |
| Space | 32 | A space is a character too |
The cells students fill in are left blank in this preview.
Try it: letters and numbers
print(ord("A"))
print(ord("a"))
print(chr(66))
word = "Hi"
for letter in word:
print(letter, ord(letter))
# ord gives the code. chr gives the character.
# Try chr(100) and ord("z").
Quick check: ASCII
Have a go
Quick check: ASCII
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
UTF-8 for every language
The world needs more than 128 characters. UTF-8 gives a code to every character in every language, and to emoji.
The 128 ASCII characters keep their old codes. So any ASCII file is also a correct UTF-8 file. Characters from other languages and emoji need more storage than an English letter.
ASCII or UTF-8?
| ASCII | UTF-8 | |
|---|---|---|
| Characters it holds | 128: English letters, digits and symbols | Every language, and emoji |
| Good for | Old files and simple English text | Web pages, apps and files that people share |
| Storage | Small | Small for English, more for other languages |
| Compatible? | Yes, every ASCII file is also UTF-8 | Yes, it works with ASCII text |
The cells students fill in are left blank in this preview.
Try it: which characters are in ASCII?
for text in ["A", "é", "日", "😀"]:
code = ord(text)
if code < 128:
print(text, code, "is in ASCII")
else:
print(text, code, "needs UTF-8")
# Add a character from your own language and run it again.
Extra: Why 128 characters?
A bit is the smallest piece of data. It is a 0 or a 1. ASCII uses 7 bits for each character. Every extra bit doubles the choices.
See the table below. With 7 bits you get 128 different codes, from 0 to 127. That is enough for English letters, digits and symbols. It is not enough for anything else.