Sahaj.blogs
21 Aug 2023 · StudyPhase 1 · Python

Python · Strings: f-strings and formatting

f-strings, the format mini-language (width, precision, alignment, thousands separators) and the = debugging shortcut.

An f-string is a string literal prefixed with f. Anything inside { } is evaluated as a Python expression and inserted into the text. PEP 498 added them in Python 3.6.

After a colon you can add a format spec: {value:spec}. The parts I practised:

  • :.2f: 2 decimal places
  • :>10 / :<10 / :^10: right, left or centre align in a width of 10
  • :,: thousands separator
  • :08.3f: pad with zeros to width 8
  • :.1%: show as a percentage
  • :b, :x: binary, hex

Writing {x=} prints the expression text and its value, which saves a lot of debugging prints.

name, marks, total = "Sahaj", 437, 500
pct = marks / total

print(f"{name} scored {marks}/{total} = {pct:.1%}")
print(f"|{name:<10}|{name:^10}|{name:>10}|")
print(f"{1234567.891:,.2f}")
print(f"{3.14159:08.3f}")
print(f"{255:b} {255:x} {255:o}")
print(f"{marks=}, {pct=:.3f}")

Output:

Sahaj scored 437/500 = 87.4%
|Sahaj     |  Sahaj   |     Sahaj|
1,234,567.89
0003.142
11111111 ff 377
marks=437, pct=0.874

#python#strings

Sources

  1. PEP 498: Literal String Interpolation — Python Software Foundation
  2. Format Specification Mini-Language — Python Software Foundation
  3. Input and Output: formatted string literals — Python Software Foundation