PY-101 ยท Strings in depth
Formatting numbers and dates
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Inside an f-string, a colon after a value adds a format spec: f"{price:,.2f}" gives 1,499.50 and f"{rate:.1%}" gives 12.5%. Dates use the datetime module and strftime codes such as %d %b %Y for 01 Dec 2026. Formatting changes how a value looks, never the value itself.
- Format decimals, thousands and percent
- Pad and align text
- Format dates with strftime
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
from datetime import dateprice = 1499.5rate = 0.125print(f"Total: โน{price:,.2f}")print(f"Discount: {rate:.1%}")for item, cost in [("Tea", 40), ("Biscuits", 120)]:print(f"{item:<10}{cost:>6}")bill_day = date(2026, 12, 1)print(bill_day.strftime("%d %b %Y, %A"))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Total: โน1,499.50
Discount: 12.5%
Tea 40
Biscuits 120
01 Dec 2026, Tuesday ๐ฏ Print the salary with thousands separators and no decimals, and the tax rate as a percentage with one decimal, like: Salary: 1,250,000 | Tax: 30.0%
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
print(f"Salary: {salary:,} | Tax: {tax:.1%}")
- The problemYour report shows 1499.5 and 0.125 and 2026-12-01. Your manager wants โน1,499.50, 12.5% and 01 Dec 2026. Same numbers, readable at a glance.
- The Python idea
f"{value:,.2f}" f"{ratio:.1%}" f"{text:>8}"Inside an f-string, a colon after a value adds a format spec: f"{price:,.2f}" gives 1,499.50 and f"{rate:.1%}" gives 12.5%. - In real lifeBanking apps format every amount with separators and two decimals, and every statement line with a readable date.
Start with a question
Your report shows 1499.5 and 0.125 and 2026-12-01. Your manager wants โน1,499.50, 12.5% and 01 Dec 2026. Same numbers, readable at a glance.
Receipts, invoices, dashboards and emails all need numbers and dates people can read instantly.
Explain it like I'm 10
Formatting is choosing the outfit for a number: the number stays the same person, it just dresses up for the occasion.
The technical explanation
.2f means two decimal places; , adds thousands separators; % multiplies by 100 and adds a percent sign.
{name:<10} pads to 10 characters left-aligned, > right-aligns, ^ centres. Great for tidy tables.
date(2026, 12, 1).strftime("%d %b %Y") formats a date. %A is the weekday name.
Syntax
f"{value:,.2f}" f"{ratio:.1%}" f"{text:>8}"
date.strftime("%d %b %Y") Real-world scenario
Banking apps format every amount with separators and two decimals, and every statement line with a readable date.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Slicing patterns โQuestions people ask
How do I format a number as currency in Python?
Use an f-string with a separator and two decimals: f"โน{amount:,.2f}". For locale-aware formats (like lakhs) use the babel library.
How do I get today's date as text in Python?
from datetime import date, then date.today().strftime("%d %b %Y").
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I format a number as currency in Python?
Use an f-string with a separator and two decimals: f"โน{amount:,.2f}". For locale-aware formats (like lakhs) use the babel library.
beginner How do I get today's date as text in Python?
from datetime import date, then date.today().strftime("%d %b %Y").
intermediate f"{3.14159:.2f}"?
3.14. Two decimal places.
intermediate f"{1234567:,}"?
1,234,567. Comma adds thousands separators.
Quick revision
Remember
- Format decimals, thousands and percent
- Pad and align text
- Format dates with strftime
- Format only when you display. Keep the real number for calculations, or rounding errors creep in when you turn text back into numbers.
Syntax
f"{value:,.2f}" f"{ratio:.1%}" f"{text:>8}"
date.strftime("%d %b %Y") Official documentation
- Format specification mini-language (docs.python.org)
- strftime codes (docs.python.org)