Formatted Output with f-strings
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Introduced in Python 3.6 (PEP 498), f-strings are the cleanest, fastest, and most idiomatic way to format text in modern Python.
Basic f-string interpolation
F-string is a string written with an f before the quotes. Each {} inside it is replaced by the value of the expression it holds.
Putting text and values together with + needs str() for every number. An f-string does it in one step. Write an f before the opening quote and put names inside {}.
student = "Aditi"marks = 94print(f"{student} scored {marks}%")Example 1 · Order line with an expression
product = "Wireless Mouse"qty = 3unit_price = 799.5print(f"{product} x {qty}")print(f"Line total: {qty * unit_price}")Wireless Mouse x 3 Line total: 2398.5
What this code does
{product}and{qty}are replaced by their values.{qty * unit_price}is calculated first, then shown:2398.5.
Python replaces each {} with the value and converts it to text for you. Any expression works inside the braces, such as {marks + 1}.
Formatting floats and decimals
Format specifier is the short code after a colon inside {} that says how to show the value: decimals, width, alignment or commas.
Prices need a fixed number of decimals. After the value, add a colon and a format specifier.
price = 49.9print(f"Amount: ${price:.2f}")Example 2 · Fixed decimals and percent
price = 1499.0discount = 0.125print(f"Price: {price:.2f}")print(f"Discount: {discount:.1%}")Price: 1499.00 Discount: 12.5%
What this code does
.2fkeeps two decimals even when the number is whole..1%turns0.125into12.5%.
The specifier :.2f rounds to two decimal places, so the output is Amount: $49.90. The f means fixed-point number.
Width, alignment and commas
Reports need columns that line up and big numbers that are easy to read. The specifier can carry an alignment, a width, a comma and decimals, in that order.
salary = 1250000print(f"Annual: ${salary:,}")print(f"{'Item':<10}{'Qty':>5}")print(f"{'Pen':<10}{12:>5}")Example 3 · Invoice total with commas
unit_price = 799.5quantity = 3total = unit_price * quantityprint(f"Total: Rs. {total:,.2f}")Total: Rs. 2,398.50
What this code does
,adds the thousands separator and.2ffixes two decimals.- The order matters: comma first, then
.2f.
Example 4 · A small aligned table
print(f"{'Item':<10}{'Qty':>5}{'Price':>10}")print(f"{'Pen':<10}{12:>5}{4.5:>10.2f}")print(f"{'Notebook':<10}{3:>5}{85.0:>10.2f}")Item Qty Price Pen 12 4.50 Notebook 3 85.00
What this code does
- The first column is left aligned in 10 characters, the others are right aligned.
- Every row uses the same widths, so the columns line up.
:, adds thousands commas. <10 pads on the right to 10 characters (left aligned) and >5 pads on the left (right aligned).
Percent, zero padding and expressions
Two more specifiers appear often. % multiplies by 100 and adds the sign. A 0 before the width pads with zeros, which suits ids and times.
rate = 0.256order_no = 7print(f"Growth: {rate:.1%}")print(f"Order #{order_no:04}")The output is Growth: 25.6% and Order #0007. For a literal brace, double it: {{ and }}.
Next step
Check your understanding
9 quick questions