Skip to content
Lesson 13 of 13

Step 1 of 9 · Reading · ~3 min

Learn

Working with Text

String Formatting

Earlier lessons dropped a value into a sentence with an f-string. This lesson covers the other half of the tool: the format spec, the part after a colon that decides how many decimal places a number shows, how wide a column is, and what pads it. By the end you can hit an exact expected output character for character.

An f-string is a string with holes in it

Write f immediately before the opening quote. Anything between { and } is Python code: it runs, its result is turned into text, and the text is dropped into that hole.

python
Dhaka got 45 mm
Double that is 90 mm

Leave the f off and nothing is substituted. The braces are printed as ordinary characters:

python
{city} got {rainfall} mm

The format spec: everything after the colon

Inside the braces you may write value:spec. The spec never changes the value, only how it is written out.

Fixed decimals with .Nf

A float prints with however many digits it happens to need, which is rarely what a report wants. f means fixed point and the number before it is the count of digits after the point:

python

Short numbers get padded with zeros, long ones get rounded. The rounding uses the binary value the machine actually stored, so an apparent tie can go the way you did not expect: f"{1.005:.2f}" gives 1.00, because 1.005 is stored as a hair less than 1.005. Nothing is broken; that is the number that is there.

Width and alignment

A number before the type is the minimum width. < left-aligns, > right-aligns, ^ centres, and d is the integer type:

python

03d is width 3 filled with zeros. Width and decimals combine in that order: {mean:8.2f} renders 7.5 as 7.50 — eight characters wide, two decimals.

Thousands separators

A , in the spec groups digits: f"{1234567:,}" gives 1,234,567 and f"{1234567:,.2f}" gives 1,234,567.00. Handy in a human report, fatal when the expected output has no commas.

Commas in print are not free

print inserts one space between each pair of arguments, including after a string that already ends in a colon:

python

When the output shape is fixed, build one string and print that single string.

Common mistakes

  • No f prefix. print("{qty} sold") prints the five characters {qty}, not the value.
  • No .2f on money. A total that lands on exactly fifty prints as 50.0, one character short of 50.00, and the line does not match.
  • A separator nobody asked for. ,.2f turns 12500000 into 12,500,000.00. Only add , when the expected output shows commas.
  • Passing pieces to print as separate arguments. print("Cost:", value) puts a space after the colon and a space from print, so the line has two spaces where one is expected.

Your exercise

Print a three-line receipt from a name, a whole quantity, and a unit price. The starter already reads the three inputs and computes the total; it prints the labels with the values missing, so every test fails until you fill the holes in. The money line is where people lose it: the quantity is an integer and prints plainly, but the total is a float and must be pinned to two decimal places.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…