Format specifications in Python
The mini-language shared by f-strings, format and __format__.
value = 1234.5678
name = "python"
print(f"{value:,.2f}|{value:>12.1f}|{value:<10.0f}|")
print(f"{name:*^12}|{name!r}|{name.upper():.3}")
print(f"{0.9765:.1%} {255:#x} {5:04d} {1e6:.2e}")
print(f"{value=:.2f}")
print("{1} then {0}".format("second", "first"))
How it works
- Width and alignment come before the type.
,groups thousands,.2ffixes decimals,%scales.=inside an f-string prints the expression and its value.
Keywords and builtins used here
print
The run, in numbers
- Lines
- 8
- Characters to type
- 258
- Tokens
- 113
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 71 seconds.
Step 3 of 3 in Numbers & text, step 61 of 72 in Language basics.