Lesson 33 of 60
โฎPrev Nextโฏ

PY-101 ยท Strings in depth

Formatting numbers and dates

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

33of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Formatting numbers and dates, running
    1. from datetime import date
    2. price = 1499.5
    3. rate = 0.125
    4. print(f"Total: โ‚น{price:,.2f}")
    5. print(f"Discount: {rate:.1%}")
    6. for item, cost in [("Tea", 40), ("Biscuits", 120)]:
    7. print(f"{item:<10}{cost:>6}")
    8. bill_day = date(2026, 12, 1)
    9. print(bill_day.strftime("%d %b %Y, %A"))
    Press โ–ถ to watch Python run this, one line at a time.
    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.
    2. The Python ideaf"{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%.
    3. 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")

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Banking apps format every amount with separators and two decimals, and every statement line with a readable date.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. f"{3.14159:.2f}"?
    2. 2. f"{1234567:,}"?
    3. 3. f"{0.25:.0%}"?
    4. 4. What does f"{x:>5}" do?
    5. 5. Which strftime code gives the full weekday name?

    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