Command Palette

Search for a command to run...

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

str, bytes and the wrong code book

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?

str, bytes and the wrong code book
Sign in to run this example in your browser.

Expected output

7 10
b'caf\xc3\xa9 \xe2\x82\xb95'
café ₹5
wrong codec: café
'\ufffd\ufffdabc'
b'caf? ?5'
b'caf 5'
<class 'str'> <class 'bytes'>
99 b'caf'