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.
Use re.finditer with a pattern containing named groups key (word characters) and value (any characters up to the next ;) to build a dictionary settings from line, with spaces stripped from each value. Print it.
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 ___ so the pattern finds numbers only when they are directly followed by kg, without including kg in the match. The program should print ['5', '12'].
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 program should list the text inside each pair of <em> tags, but it returns one long string. Fix the pattern so it prints ['soon', 'free', 'today'].
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 word and pos each time one of them changes, once both exist.
import re
for m in re.finditer(r"[A-Z]\w*", "see Ana and Bo in Rome"):
word = m.group()
pos = m.start()Sign 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: One re.sub with a replacement function: no split and no for loop.
This code works, but it splits the text and rebuilds it by hand to convert prices from dollars to cents. Rewrite it with one re.sub call that uses a replacement function, with no split and no for loop. 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 render(template, values) that replaces placeholders like {{ name }} (spaces inside the braces optional) with values[name], using re.sub with a function. Unknown placeholders stay exactly as written. It returns the text and a sorted list of the unknown names. The program should print:
Hi Ana, your order #42 ships Friday. {{ coupon }}
missing: ['coupon']
No placeholders here.
missing: []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.