Skip to content
Start free

What is UTF-8, and why does an "é" take two bytes?

Predict how many bytes an accented letter takes in UTF-8 and in Windows-1252, then run it and see the difference.

Free course. Your Maestro, an AI, .

To understand "Mérida" you have to go one level down, to what's really on the disk. A computer doesn't store letters: it stores bytes, numbers from 0 to 255. To store text it needs a table that says which byte or bytes stand for each letter. That table is called an encoding.

Two common encodings in these examples are UTF-8, which can write the characters of any language, and Windows-1252, an encoding that uses one byte per representable character used by many older programs and systems on Windows. In Python they're called "utf-8" and "cp1252". For ASCII characters, such as the unaccented Latin letters in these examples, the two store the same bytes. For é, ñ and ü, they don't.

Before any explanation, predict. In each program, len counts how many items something has, and .encode(...) turns text into its bytes using the table you name. Write down what you think each one prints and why; then run it.

Four short programs. Each one changes just one thing from the one before.

One letter in UTF-8

Example 1 of 4

example.py
letra = "é"print(len(letra))print(letra.encode("utf-8"))print(len(letra.encode("utf-8")))

A guess is fine. What you write stays in this browser and waits in your course.

Example 2 of 4, opens once you explain this one

Here's what you saw, in one table, with what Python printed:

TextUTF-8Windows-1252
eb'e', 1 byteb'e', 1 byte
éb'\xc3\xa9', 2 bytesb'\xe9', 1 byte
ñb'\xc3\xb1', 2 bytesb'\xf1', 1 byte
Mérida7 bytes6 bytes

Three ideas to hold on to:

  1. The encoding belongs to the file, not to the letter. The é is the same letter in both cases; what changes is how it was written as bytes. A file doesn't store "accented letters", it stores the bytes a table chose.
  2. Without accents, you can't tell. A file with only names like Juan or Tonala looks the same in both tables. That's why the problem shows up out of nowhere, the day the first Tonalá or Peña arrives.
  3. Reading means applying a table. To turn bytes into letters, the program has to use the same table the file was written with. The file doesn't tell it which. If it uses another one, other letters come out: that's the next page.

Latin-1 (also called ISO-8859-1) is almost a twin of Windows-1252: for Spanish letters they give the same bytes, and they differ in symbols like the euro. If a program offers "Latin-1" for reading a Windows-1252 file with Spanish text, it usually works. If you can't keep them straight, ask your Maestro.

Try it

Replace the word in the program with made-up names, such as Ciudad del Peñón or Papelería La Cigüeña. Don't use real customer names or confidential information. Find one that takes the same bytes in both tables and one that doesn't. Try an emoji too, and read what Python tells you.

Your turn

Run the program. Then change palabra to "Tlajomulco de Zúñiga" and run it again: how many more bytes does it take in UTF-8 than in Windows-1252, and why? Try a word without accents and an emoji. You're done when you can explain in your own words where each difference comes from.

When the letter doesn't fit in the table

So far both tables knew every letter you tried. But Windows-1252 gives each letter a single byte, and one byte can only tell 256 values apart: there's no room for everything that exists. UTF-8 has room, because it uses more bytes when it needs to. Try a symbol that isn't a Spanish letter, a coffee cup next to the word:

Python
palabra = "Café ☕"print(palabra, "tiene", len(palabra), "letras")print("UTF-8:       ", palabra.encode("utf-8"), len(palabra.encode("utf-8")), "bytes")try:    print("Windows-1252:", palabra.encode("cp1252"))except UnicodeEncodeError as error:    print("Windows-1252: no cabe,", error.reason)
Output
Café ☕ tiene 6 letrasUTF-8:        b'Caf\xc3\xa9 \xe2\x98\x95' 9 bytesWindows-1252: no cabe, character maps to <undefined>
Run in Maestro's sandbox. The program's messages are in Spanish: no cabe means it doesn't fit.Ran on October 17, 2026 by Maestro sandbox run (check_exercise_code).

UTF-8 wrote the cup as three bytes, E2 98 95, and Windows-1252 couldn't: Python stops with an error instead of writing something else in its place. That's why, when you write a file, UTF-8 is the safe bet, and Windows-1252 only makes sense if the system that will read it requires it. You'll come back to this in module 4. If your file has symbols like the euro or an emoji, ask your Maestro which encoding to use.

Check yourself: How many bytes does the word "Año" take when saved in UTF-8?