typestar

f-string formatting in Python

Python's inline template strings, with formatting built in.

name = "Marie"
score = 0.91234
count = 1234567

banner = f"Welcome back, {name}!"
percent = f"accuracy: {score:.1%}"
padded = f"|{name:>10}|"
grouped = f"{count:,} rows"

debug = f"{score=}"
math = f"double: {score * 2:.3f}"

How it works

  1. Interpolates any expression inside {} braces.
  2. Format specs after : handle percent, padding, commas.
  3. {score=} prints the expression and its value for debugging.

The run, in numbers

Lines
11
Characters to type
224
Tokens
76
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 54 seconds.

Type this snippet

Step 1 of 9 in Strings, step 7 of 72 in Language basics.

← Previous Next →