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 | A small, old set of 128 codes for English letters, digits and symbols. | A is 65 |
| UTF-8 | A modern way to store every character in every language. | é, 日, 😀 |
| Byte | A unit that measures storage. | A short word takes a few bytes |
| Encoding | A rule that turns text into codes to save or send. | 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").
Make a free account to save your work and track your progress.
Quick check: ASCII
Have a go
Quick check: ASCII
3 questionsUTF-8 for every language
The world needs more than 128 characters. UTF-8 can store every character in every language, and emoji too. Each one still has its own number.
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.
Make a free account to save your work and track your progress.
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.
Extra: Counting the choices
| Bits | How we count | Different codes |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 2 + 2 | 4 |
| 3 | 4 + 4 | 8 |
| 4 | 8 + 8 | 16 |
| 5 | 16 + 16 | 32 |
| 6 | 32 + 32 | 64 |
| 7 | 64 + 64 | 128 |
| 8 | 128 + 128 | 256 |
The cells students fill in are left blank in this preview.
Extra: Bytes per character in UTF-8
| Character | Where it is from | Bytes |
|---|---|---|
| A | English letter | 1 |
| é | French letter with an accent | 2 |
| 日 | Japanese character | 3 |
| 😀 | Emoji | 4 |
The cells students fill in are left blank in this preview.
Extra: Try it: count the bytes
# encode turns text into bytes. len counts them.
for text in ["A", "é", "日", "😀", "Hello"]:
data = text.encode("utf-8")
print(text, "has", len(text), "characters and", len(data), "bytes")
# Add your own name to the list and run it again.
Make a free account to save your work and track your progress.
Which characters are in ASCII?
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
The wrong encoding spoils text
Warning
Many people think text looks the same wherever you open it. It looks the same only when the reader uses the same encoding as the writer. A UTF-8 file read another way shows symbols such as é for é. So say which encoding you use. Use UTF-8 on the web and in new files.
Saving and reading text
Your turn: codes and letters
Fill in the four functions with ord and chr. 1. char_code gives the code of one letter. 2. next_letter gives the letter after it. 3. secret_word turns a list of codes into a word. 4. is_ascii gives True if every character has a code below 128. The tests use real characters from French and Japanese. Press Run Tests to check.
def char_code(letter):
# Give back the ASCII code of one letter. "A" is 65.
return None
def next_letter(letter):
# Give back the letter after this one. "a" gives "b".
return None
def secret_word(codes):
# codes is a list of numbers. Give back the word they spell.
# [72, 105] gives "Hi".
return None
def is_ascii(text):
# Give back True if every character has a code below 128.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: letters as numbers
Press Play on the short film below. It shows how ord and chr turn letters into codes and back, how a word is stored, and which characters need UTF-8. Listen for the myth about the character 0.
Film: ASCII and UTF-8
ASCII and UTF-8
Every scene, as text
- How does a computer store a letter? A computer only stores numbers. print(ord("C")) gives the code of the letter C (character: a letter, digit, symbol or emoji), and it prints 67 (character code: the number for a character).
- ASCII has 128 characters, for English letters, digits and symbols. print(ord("A")) prints 65 (ord: gives the code). print(chr(66)) prints B (chr: gives the character). B comes after A (next letter: the next number).
- word is set to "Hi". The loop prints each letter with its code (prints: each letter and its code): H 72 and i 105 (a word: a code for every character).
- Myth (false): the character 0 is stored as zero. That is not true. print(ord("0")) prints 48 (48: the code of the character 0). print(ord(" ")) prints 32 (32: the code of a space, a character too).
- UTF-8 for every language: it stores every character in every language, and emoji too. The loop gives the code of A, é and 😀; if the code is below 128 (below 128: in ASCII), it prints is in ASCII, else needs UTF-8. It prints A 65 is in ASCII (A: still 65, its old ASCII code), é 233 needs UTF-8 and 😀 128512 needs UTF-8 (é and 😀: need UTF-8). Use UTF-8 for new work (new work: use UTF-8).
Final check
Have a go
Final check
5 questionsRemember
Important
Every character has a number. ASCII has 128 characters. UTF-8 has every language and emoji. Use UTF-8 for new work.