Chapters
Python114 chapters

Exercises

Text and Unicode

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Print how many characters the word has, then how many bytes it takes in UTF-8.

Python
word = "Müller"
# print the character count, then the byte count
Exercise 2

Decode the bytes back to text, and print the result.

Python
data = "café".encode("utf-8")
# print the text again
Exercise 3

These bytes are not valid UTF-8. Decode them without crashing, keeping a marker for what was lost.

Python
data = b"caf\xff"
# print a decoded version that does not raise