Lab: text and numbers
Practice functions and strings by building small, real text utilities — and check your work as you go.
- Combine functions and string methods to solve real tasks
- Convert between text and numbers safely
- Build confidence writing code that passes tests
This is an optional lab. No new concepts — just hands-on practice with the functions and strings you've met so far. Write code, run it, and use the Check buttons to test yourself. You learn this by doing it.
Real programs are mostly small transformations of text and numbers glued together. Let's build a couple, the way you actually would — try, run, see what breaks, fix it.
Warm up: experiment freely
Before the graded parts, just play. Edit this and run it — try different methods, break it, see what the errors say:
Checkpoint 1 — initials
Here's a real task: turn a full name into its initials. Think about the steps —
split the name into words, take the first letter of each, upper-case them, join
them. Write initials so the checks pass.
Write initials(full_name) that returns the upper-case initials of each word. "ada lovelace" -> "AL".
initials("ada lovelace") → "AL"initials("grace brewster hopper") → "GBH"Stuck? A list comprehension over full_name.split() taking word[0] is one
clean way — but a plain loop is perfectly good too. Whatever passes the checks.
Checkpoint 2 — parse a price
Input from the outside world arrives as text, even when it's really a number.
Convert a price like "$3.50" into a whole number of cents (350). You'll
need to strip the $, turn the rest into a number, and scale it.
Write price_to_cents(text) that turns a price string into an int number of cents. "$3.50" -> 350. The dollar sign is optional.
price_to_cents("$3.50") → 350price_to_cents("0.99") → 99Watch the conversion: float("3.50") * 100 is 350.0, a float — the checks want
an int. Wrap it in round(...) to get a clean whole number, and you'll also
dodge floating-point surprises like 349.99999.
Done?
If both checkpoints are green, you just wrote two genuinely useful functions from nothing but the basics. That's the whole game — small, correct pieces you can trust. Next up in the module: collecting many values with lists and tuples.