Practice · Python

Practice String Formatting & Parsing

Format numbers and text for display (f-string spec, alignment, rounding) and parse structured text into typed values.

String Formatting & Parsingwarming python…

Format Space Trade Transactions

Write a function format_transactions that receives a list of strings, each representing a trade in the form "item=price". The price may be an integer or a float and may have surrounding whitespace. Your function must:

  1. Parse each line into a name and a numeric price.
  2. Return a single string containing one line per trade.
  3. Each line should have the name left‑aligned in a 10‑character field, followed by a dollar sign and the price right‑aligned in a 7‑character field, shown with exactly two decimal places.
  4. Lines are separated by "\n"; an empty input list should produce an empty string.

Example

format_transactions(["apple=1.5", "banana=2"]) 
# returns "apple     $   1.50\nbanana    $   2.00"
Press Run to see output, or Check to grade against the tests.

Want a whole course built around string formatting & parsing?

Start my path — free

Adaptive practice, streaks, projects with real code review. Free tier gates AI help, never content.