Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.4 — Unicode, Bytes and Encodings
Python · Example 1 of 3

Code points and UTF-8 byte counts

One character, one code point, 1 to 4 bytes. Add your own character to the list.

A Python str is a sequence of Unicode code points (numbers that stand for characters), while bytes is a sequence of raw numbers from 0 to 255. Turning text into bytes is encoding and the reverse is decoding; almost every mysterious bug with accents, emoji or é garbage is an encoding mismatch.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Print how many characters and how many UTF-8 bytes are in the text "Hectal ₹".

02

Make mojibake by decoding the UTF-8 bytes of "café" as Latin-1, print it, then repair it.

03

Print the letter that is 3 places after "a" using ord and chr, and the letter 25 places before "z".

Explain it without notes

01

What is the difference between a character, a code point and a byte?

02

Why can len(text) differ from len(text.encode("utf-8"))?

03

What causes UnicodeDecodeError, and what does é in text tell you?

04

Why can two strings that print the same be unequal, and how do you fix it?

Code points and UTF-8 byte counts
Sign in to run this example in your browser.

Expected output

A code point 65 0x41 bytes 1 b'A'
é code point 233 0xe9 bytes 2 b'\xc3\xa9'
₹ code point 8377 0x20b9 bytes 3 b'\xe2\x82\xb9'
日 code point 26085 0x65e5 bytes 3 b'\xe6\x97\xa5'
😀 code point 128512 0x1f600 bytes 4 b'\xf0\x9f\x98\x80'
A ₹ 😀
₹ 😀