Why Format Strings?
In this tutorial, we'll understand why formatted strings are easier than manual joining.
String formatting means building text that includes values.
You have already seen this style:
name = "Ada"
score = 95
print("Name:", name)
print("Score:", score)
That works well for simple output. But sometimes you want one clean sentence:
Ada scored 95 points.
The Awkward Way
You can join strings with +, but it gets clumsy:
name = "Ada"
score = 95
message = name + " scored " + str(score) + " points."
print(message)
The str(score) is needed because Python cannot join a string and an integer directly.
The Better Idea
Formatted strings let you put values inside text more naturally:
name = "Ada"
score = 95
message = f"{name} scored {score} points."
print(message)
This is easier to read. The text looks close to the final output.
When Formatting Helps
String formatting is useful for:
- Messages to users
- Receipts
- Reports
- Debug output
- File content
- Labels for values
Example:
price = 4.99
quantity = 3
print(f"{quantity} items cost {price * quantity}")
Practice
Create variables for a name, age, and favourite language. Print one sentence that includes all three.