Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

ASCII and UTF-8

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

40 minutes

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

WordWhat it meansExample
CharacterOne letter, digit, symbol or emoji.A, 7, ?, 😀
Character codeThe number a computer uses for a character.A is 65
ASCIIAn old, small set of 128 character codes. It covers English letters, digits and symbols.A is 65
UTF-8A modern way to store every character in every language.é, 日, 😀
ByteA unit of storage. Files are measured in bytes.A short word takes a few bytes
EncodingThe 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

CharacterCodeNote
A65Capital letters start at 65
B66The next letter is the next number
a97Small letters start at 97
048The character 0. It is a character, not the number zero.
Space32A space is a character too

The cells students fill in are left blank in this preview.

Try it: letters and numbers

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your 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?

ASCIIUTF-8
Characters it holds128: English letters, digits and symbolsEvery language, and emoji
Good forOld files and simple English textWeb pages, apps and files that people share
StorageSmallSmall for English, more for other languages
Compatible?Yes, every ASCII file is also UTF-8Yes, it works with ASCII text

The cells students fill in are left blank in this preview.

Try it: which characters are in ASCII?

Code: python
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.

The lesson goes on

10 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.