So far, you’ve learnt:
- Part 1: The basics — quotes, printing, joining, length, and indexes.
- Part 2: Escapes, multi-line strings, and slicing.
- Part 3: String methods for cleaning and transforming text.
Now it’s time for Part 4: String Formatting.
Formatting means combining variables and strings in neat, readable ways. Instead of messy concatenation with +, formatting lets you create sentences that look natural while still including dynamic values like names, numbers, and results from calculations.
The problem with +
At first, you might try joining text with +. This works fine if everything is already text:
first = "Hello"
second = "World"
print(first + " " + second)
But problems appear when you mix in numbers (like ages, scores, or prices).
name = "Alice"
age = 12
# ❌ This will cause an error
print("My name is " + name + " and I am " + age + " years old.")
Why? Because Python won’t automatically convert the number 12 into a string.
You can fix it by converting manually with str(age):
print("My name is " + name + " and I am " + str(age) + " years old.")
This works, but it’s clunky and hard to read. Luckily, Python gives us better ways.
F-strings (the easy way)
F-strings are the most modern and beginner-friendly solution. The idea is simple:
- Put an f right before the quotes.
- Drop variables directly into the string using
{ }.
name = "Alice"
age = 12
print(f"My name is {name} and I am {age} years old.")
No need to convert numbers or break up the sentence — Python handles it all.
You can even put expressions inside the curly braces:
print(f"Next year I will be {age + 1}")
This makes your output short, clean, and very readable.
Try this
- Make variables for your name and favourite colour.
- Print a sentence like:
"My name is ___ and my favourite colour is ___." - Add a variable for your age and include it in the same sentence.
Using .format()
Before f-strings were introduced, Python used the .format() method. It’s still common in older tutorials and codebases, so it’s worth knowing.
name = "Bob"
age = 20
print("My name is {} and I am {} years old.".format(name, age))
The { } placeholders are replaced by the values you give to .format(), in the same order.
You can also reorder or repeat values using index numbers inside the braces:
print("First: {0}, Second: {1}, First again: {0}".format("A", "B"))
This flexibility is why .format() was popular — but for new learners, f-strings are simpler.
Try this
- Print
"I like apples and oranges"using.format(). - Swap the order so it prints
"I like oranges and apples".
Old-style % formatting
Python also has an older style that uses % signs. You may see it in very old code, but it’s mostly replaced by .format() and f-strings.
name = "Charlie"
age = 15
print("My name is %s and I am %d years old." % (name, age))
Here:
%sis for strings%dis for integers (whole numbers)%fis for floating point numbers (decimals)
This style can look confusing, so don’t worry if it feels odd. Just know it exists.
Formatting numbers
Formatting isn’t only for inserting variables — it can also control how numbers appear.
With f-strings you can:
- Round numbers to a set number of decimal places.
- Add commas for large numbers.
Examples:
price = 12.3456
print(f"Price: {price:.2f}") # Price: 12.35 (rounded to 2 decimal places)
big_number = 1000000
print(f"{big_number:,}") # 1,000,000 with commas
This is very useful for things like money, scores, and reports, where presentation matters.
Try this
- Make
pi = 3.14159265. Print it rounded to 2 decimal places. - Make
big = 1234567890. Print it with commas.
Putting it all together
Here’s a short program that uses f-strings to format text and numbers neatly:
name = "Daisy"
score = 97.456
rank = 1
print(f"Congratulations {name}!")
print(f"Your score is {score:.1f} points.")
print(f"You are ranked #{rank:,} in the world!")
Output:
Congratulations Daisy!
Your score is 97.5 points.
You are ranked #1 in the world!
See how much easier it is to read and understand compared to using + everywhere.
Try this
Write a program that:
- Asks the user for their name and age.
- Prints:
"Hello NAME, next year you will be AGE+1."using an f-string.
Summary
+works for joining text but is clumsy with numbers.- F-strings are the modern, easiest way to combine variables and text.
.format()is older but still useful to recognise.%formatting is very old, mostly replaced now.- F-strings can also format numbers (rounding, commas, etc.).
✅ With string formatting, you can now build clear, professional-looking messages, reports, and outputs in your Python programs.










