TalentJR

🐍 Python for kids · Class 7 · Lesson 9 of 18 · Free

Advanced f-Strings – Decimals, Commas and Neat Columns

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Put expressions, methods and dictionary values inside f-strings.
  • Format numbers with decimal places, commas, zeros and percentages.
  • Line up text in neat columns with width and alignment.

Your program says the average is 83.33333333333333. A real report card says 83.33. Today you will make Python print numbers and tables that look neat and professional.

What can I put inside the curly brackets of an f-string?

You already know f"Hello {name}". The f tells Python to fill in the curly brackets.

Inside the brackets you can write any expression: a sum, a method call or a function like len().

Python works out the value first, and then puts it into the text.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶name = "priya"
2marks = [78, 92, 85]
3print(f"Student: {name.title()}")
4print(f"Tests taken: {len(marks)}")
5print(f"Total: {sum(marks)}, best: {max(marks)}")
6print(f"Next year she will be in Class {7 + 1}.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Student: Priya
Tests taken: 3
Total: 255, best: 92
Next year she will be in Class 8.

How do I round a number to 2 decimal places in an f-string?

Add a colon after the value, then a format code. :.2f means two digits after the decimal point.

The f in .2f stands for fixed-point, a number with a fixed count of decimals.

This only changes how the number looks. The variable itself keeps its full value.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶marks = [78, 92, 80]
2avg = sum(marks) / len(marks)
3print(f"Raw: {avg}")
4print(f"Two decimals: {avg:.2f}")
5print(f"One decimal: {avg:.1f}")
6print(f"No decimals: {avg:.0f}")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Raw: 83.33333333333333
Two decimals: 83.33
One decimal: 83.3
No decimals: 83

💡 :.2f is perfect for money too. A price of 49.5 shows as 49.50, just like on a bill.

How do I add commas to big numbers in Python?

Big numbers are hard to read without commas. The format code :, adds them for you.

You can join codes together. :,.2f gives commas and two decimals at the same time.

Python groups digits in threes, the international way, not in lakhs and crores.

🐍 Python — you can edit this code
Output
India's population: 1,428,627,663
Prize money: Rs 2,500,000.50
Lakh in digits: 100,000

💡 The quote mark in India's is fine here, because the f-string itself uses double quotes.

How do I line up text in columns with f-strings?

A number after the colon sets a width. {name:10} always uses 10 spaces, filling the rest with blanks.

The signs < > ^ choose the side. < means left, > means right and ^ means centre.

Words usually go on the left and numbers on the right. Then a table looks tidy.

🐍 Python — you can edit this code
Output
Name       Marks
----------------
Aarav       78.5
Diya        92.2
Kabir       60.0
[   top   ]

💡 Width and decimals can work together. {mark:>8.1f} means width 8, right side, one decimal.

How do I show percentages and pad numbers with zeros?

:% turns a fraction into a percentage. It multiplies by 100 and adds the % sign.

For roll numbers or ticket numbers, :03d pads a whole number with zeros to 3 digits.

So roll number 7 prints as 007. The d means a whole number, with no decimals.

🐍 Python — you can edit this code
Output
Score: 85%
Score: 85.0%
Roll number 007
Roll number 042
Roll number 105

How do I use dictionary values and curly brackets in f-strings?

Inside an f-string with double quotes, write dictionary keys with single quotes, like user['name'].

To print a real curly bracket, type it twice: {{ or }}. Python then shows just one.

Adding = after a variable, like {age=}, prints the name and value. This is handy for finding bugs.

🐍 Python — you can edit this code
Output
Bilal lives in Lucknow.
Python sets use { and }.
age=13

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

price = 49.5
print(f"Rs {price:.2f}")

Question 2. What does this print?

n = 7
print(f"Roll {n:03d}")

Question 3. What does this print?

print(f"[{'hi':>5}]")

🧩 Your challenge

Print a neat shop bill. Ask for the customer's name. Then print each item with its quantity and cost in lined-up columns, with costs to 2 decimal places. End with the grand total.

Expected output (with the inputs given):
Customer name: Meera
Bill for Meera
Notebook         4    182.00
Pen             10    120.00
Geometry box     1    120.75
----------------------------
Total                 422.75
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

total = 250
print("Total: {total}")

No error message – but it prints Total: {total}, not Total: 250.

✅ Right

total = 250
print(f"Total: {total}")

Without the f before the quote, Python treats the brackets as plain text. The f switches filling on.

❌ Wrong

marks = "85"
print(f"{marks:.1f}")

ValueError: Unknown format code 'f' for object of type 'str'

✅ Right

marks = "85"
print(f"{float(marks):.1f}")

The .1f code is only for numbers. Text from input() is a string, so change it with float() first.

❌ Wrong

pi = 3.14159
print(f"{pi:.2}")

No error message – but it prints 3.1, not 3.14.

✅ Right

pi = 3.14159
print(f"{pi:.2f}")

Without the f, .2 means two digits in total. With .2f you get two digits after the decimal point.

📌 Remember

  • Any expression can go inside f-string brackets: sums, methods and function calls.
  • A colon starts a format code, like {avg:.2f} for two decimal places.
  • :, adds commas to big numbers, and :.1% shows a percentage.
  • :03d pads a whole number with zeros, like 007.
  • A width with < > or ^ lines text up on the left, right or centre.
  • Formatting only changes how a value looks, never the value itself.

Finished the quiz and the challenge?

Questions students ask

Does :.2f change the value stored in my variable?

No. It only changes how the number is shown. Use round() if you need a rounded value for more maths.

Can I show numbers in lakhs and crores with an f-string?

Not directly. The :, code groups digits in threes. Indian grouping needs a small function of your own.

Why do I get a ValueError when formatting a number?

The value is probably a string, often from input(). Change it with int() or float() before using a number code.

Are f-strings better than joining strings with +?

Usually yes. They are shorter, easier to read and work with numbers without calling str().