typestar

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

  1. Width and alignment come before the type.
  2. , groups thousands, .2f fixes decimals, % scales.
  3. = inside an f-string prints the expression and its value.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Numbers & text, step 61 of 72 in Language basics.

← Previous Next →