Combining Text and Variables Cleanly & Controlling Number Formats
When building apps, games, or websites, you constantly need to display messages that include stored information—like "Welcome back, Sarah!" or "You have 3 new notifications." In older versions of Python, combining text and variables was clunky and hard to read. Fortunately, modern Python gives us the cleanest, easiest tool for this job: formatted string literals, affectionately known as f-strings.
To create an f-string, you simply put the letter f right in front of the opening quotation mark, like f"Hello, {name}!". The magic happens inside the curly braces {}: whenever Python sees curly braces inside an f-string, it pauses, looks up the variable or calculation inside, and replaces the braces with the actual value. It is just like filling in the blanks on an invitation card.
Beyond inserting simple variables, f-strings also allow you to format numbers with precision. When working with money or measurements, decimals often end up with messy repeating digits (like 19.9999999). Inside curly braces, you can add a colon (:) followed by formatting rules: writing {price:.2f} tells Python: "Format this number as a decimal with exactly 2 digits after the point." For example, 89.995 becomes 90.00.
You can also control spacing and text alignment using the colon rules: {product:<12} left-aligns text in a space 12 characters wide, while {quantity:^6} centers text in a space 6 characters wide. This lets you print beautifully aligned receipts, tables, and reports directly in your console with zero extra libraries.
Real-World Analogy
Think of an f-string like a printed certificate with blank spaces. The certificate has pre-printed text, and inside the brackets {student_name} and {score}, Python writes in the specific details before handing the final sheet to the reader!
Code ExamplePython 3
# f-Strings (formatted string literals) allow you to inject# variables and calculations directly into text using curly braces {}.
name = "Taylor"
points = 250
level = 4# Put an 'f' before the opening quote:
message = f"Player {name} is at level {level} with {points} points!"print(message)
# You can even do math right inside the curly braces:
price = 49.50print(f"Total after 10% discount: ${price * 0.90:.2f}")
Key Rules & Concepts
f-strings start with the letter f before the quotation marks, like f"Hello {name}".
Place variables and expressions inside curly braces {} to inject their values into the string.
Use :.2f inside curly braces to format numbers to exactly two decimal places (perfect for currency).
Use :<10 to left-align text in a column 10 characters wide, or :^6 to center it in 6 characters.
f-strings make your code much easier to read and maintain compared to older concatenation methods.
Challenge
Beginner
Format an invoice line item using an f-string to align product details, quantities, and rounded currency values into a neat table row.
What to do:
Given product = "SSD 1TB", quantity = 2, and price = 89.995
Format the line with product left-aligned within 12 characters ({product: <12})
Add a column separator " | "
Center the quantity within 6 characters ({quantity: ^6})
Add a column separator " | $"
Format the price to 2 decimal places ({price: .2f})
Print the completed formatted line
Reference Solution Locked
0 Attempts
Solve the challenge above and run your code to verify it. If you are stuck, you can unlock the reference solution after running your code at least once.