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

Free lesson

6.5 (1 of 3): ASCII and UTF-8

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

40 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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
ASCIIA small, old set of 128 codes for English letters, digits and symbols.A is 65
UTF-8A modern way to store every character in every language.é, 日, 😀
ByteA unit that measures storage.A short word takes a few bytes
EncodingA 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

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").

Make a free account to save your work and track your progress.

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

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

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.

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

BitsHow we countDifferent codes
122
22 + 24
34 + 48
48 + 816
516 + 1632
632 + 3264
764 + 64128
8128 + 128256

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

Extra: Bytes per character in UTF-8

CharacterWhere it is fromBytes
AEnglish letter1
éFrench letter with an accent2
日Japanese character3
😀Emoji4

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

Extra: Try it: count the bytes

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

A flowchart: the "Text: Café" is "saved as UTF-8" in a "File or message"; "read as UTF-8" it gives back the "Text: Café", and "read the wrong way" it gives the "Wrong text: é".A flowchart: the "Text: Café" is "saved as UTF-8" in a "File or message"; "read as UTF-8" it gives back the "Text: Café", and "read the wrong way" it gives the "Wrong 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.

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

  1. 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).
  2. 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).
  3. 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).
  4. 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).
  5. 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 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

Remember

Important

Every character has a number. ASCII has 128 characters. UTF-8 has every language and emoji. Use UTF-8 for new work.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.