Format specs in f-strings ({x:.2f})
After the colon inside an f-string's braces, you tell Python how to display the value: decimals, width, alignment.
Used in 128 lessons of the course, from 00.02.
Inside {} in an f-string you can write {value:spec}. What follows the : is the format spec. It does not change the value, only how it becomes text.
Common pieces:
.2f: a float with 2 decimals.<16: left-align in a width of 16 characters (>right,^center).,: thousands separator..1%: percentage.
It is written this way so the formatting sits next to the value, and you read the line left to right without looking elsewhere.
In lesson 00.02 the course uses print(f"{name:<16} {installed_version(name)}"). The <16 pads each name to 16 characters, so the versions line up in a column.
print(f"{3.14159:.2f}")
Reach for it when: you want to show numbers with a set precision or line up text in columns when printing.
Examples
Common mistake
x = "3.14159"
print(f"{x:.2f}")Why: `.2f` is for numbers. A string cannot be formatted as a float, even if it looks like a number.
How to write it: Convert first: f"{float(x):.2f}".
Common mistake
n = 5
print(f"{n:>4}|{n:<4}|")Why: The direction is easy to mix up: `>` pushes the value to the right (padding on the left) and `<` keeps it on the left.
How to write it: Think of the arrow: it points to where the text ends up. Use < for text in columns.
