Code of the Day
BeginnerCore syntax

Lab: text and numbers

Practice functions and strings by building small, real text utilities — and check your work as you go.

Lab · optionalPythonBeginner15 min
Recommended first
By the end of this lesson you will be able to:
  • 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:

Python — editable, runs in your browser

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.

Name to initialsPython

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.

Price text to centsPython

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")99

Watch 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.

Finished reading? Mark it complete to track your progress.

On this page