Lesson 16 of 21 · 48 min · UG / professional
Formatted output people can audit
Units on the label; one number in the function. format(), templates and column widths live on a laptop. The kiosk and the CSV must not invent two different roundings.
School notice board
print() is how your program speaks.
You will be able to
- ✓ Put units on the label; keep one number in the function
- ✓ Use f-strings for lines a clerk can audit
- ✓ Know format() and templates as laptop tools for widths and placeholders
Accounts will not read a pile of floats
“236” without “INR” or “incl GST” is a number that can be pasted into the wrong column. f-strings fill holes from names: f"Bill INR {bill} incl GST". Pretty is not decoration; it is a contract. One function returns 236.0. The kiosk prints the audit line. The CSV writer writes 236. Two presentations, one rule.
If you format inside the only GST function, a chart library receives a string and you have to parse it back — a gift to bugs. Return the number. Format at the edge that talks to humans or files.
Widths and templates live on a laptop
format(bill, ".2f"), f"{name:10} {mark:5}" and string.Template("$college $roll") are for aligned columns and for text that non-programmers edit. This sandbox keeps f-strings and labels. Copy the width sample when you print a fee table the principal will hold next to a wall notice.
The kiosk and the CSV must not invent two different roundings. Round once, in one function, then present. Paise in the function, rupees on the label if that is the notice — but not two independent roundings.
Laptop: format widths and a template
bill = 236
print("{:.2f}".format(bill))
print(f"{'Qty':6} {'INR':>8}")
from string import Template
print(Template("Bill $n incl GST").substitute(n=bill))
Words that matter
- Presentation
- How a value is shown — units, rounding, column order.
- Audit line
- A print a clerk can match to a notice on the wall.
- format / Template
- Laptop tools for widths, decimals and $placeholders.
Common mistakes
Avoid: Returning "INR 236 incl GST" from add_gst.
Do this: Return 236; format at the edge.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — unit on the label
Change 236. The word INR stays so the column cannot be mistaken.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — one number, two lines
The function-shaped total is reused. Only the labels differ.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — A three-column sketch a clerk can read
Each print is one row. Names match the notice.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Label the free seats
free = 4. Print a line that includes both Free and 4.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.