Lab, Text and Data
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write byte_report(text) that returns a string like naïve: 5 chars, 6 bytes, 1 non-ASCII, counting characters, UTF-8 bytes and characters whose code point is above 127. Print the report for "naïve", "plain" and "€10".
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ with the method call that turns the bytes back into text, so the program prints <class 'bytes'> 7 and then Zoë ok.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The names arrive as UTF-8 bytes, but the program prints garbled text like Françoise. Fix the decoding so it prints:
José Françoise Øyvind
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows b and multi each time one of them changes, once both exist. multi counts bytes that belong to a multi-byte character (128 or more).
multi = 0
for b in "aé!".encode("utf-8"):
if b >= 128:
multi = multi + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Build dump with data.hex(" "): no for loop.
This code works, but it builds the hex dump with a loop. Replace the loop with one call to the bytes.hex method, using a space separator. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write fit(text, limit) that returns text unchanged if its UTF-8 form is at most limit bytes. Otherwise it returns the longest start of the text that, with ... added, fits in limit bytes, never splitting a character. Print each result and its byte length. The program should print:
'hello' 5 'Grüße...' 10 '日本...' 9 '...' 3
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.