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.
Print how many characters and how many UTF-8 bytes are in the text "Hectal ₹".
Make mojibake by decoding the UTF-8 bytes of "café" as Latin-1, print it, then repair it.
Print the letter that is 3 places after "a" using ord and chr, and the letter 25 places before "z".
Explain it without notes
What is the difference between a character, a code point and a byte?
Why can len(text) differ from len(text.encode("utf-8"))?
What causes UnicodeDecodeError, and what does é in text tell you?
Why can two strings that print the same be unequal, and how do you fix it?
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'