Home Tutorials Core Syntax

Core Syntax

f-Strings Formatting: Everything You Can Put Inside the Braces

Pyford Notes July 1, 2026 7 min read
Key points
  • Prefix a string literal with f or F to enable f-string interpolation.
  • Curly braces {} evaluate any Python expression at runtime.
  • A colon after the expression introduces a format spec: {value:.2f}.
  • The = specifier produces self-documenting debug output: {x=} prints x=42.

Basic syntax

An f-string is a string literal prefixed with f. Any expression inside {} is evaluated at runtime and its string representation is inserted:

name = "Ada"
age = 36
print(f"Name: {name}, Age: {age}")
# Name: Ada, Age: 36

F-strings replaced % formatting and .format() calls for most use cases because the expression lives right next to the surrounding text, making the connection obvious.

Arbitrary expressions inside braces

The expression slot accepts any valid Python expression, not just variable names:

items = [1, 2, 3, 4, 5]
print(f"Total: {sum(items)}, Average: {sum(items) / len(items):.1f}")
# Total: 15, Average: 3.0

import datetime
print(f"Year: {datetime.date.today().year}")
# Year: 2026

Avoid putting complex logic inside an f-string. If the expression requires more than one read to understand, compute the value first and assign it a descriptive variable name.

The format spec mini-language

After a colon, you can write a format specification that controls how the value is rendered. The colon itself is not part of the expression; everything after it is the spec:

pi = 3.14159265
print(f"{pi:.4f}")    # 3.1416   (four decimal places)
print(f"{pi:10.3f}")  # '     3.142'  (width 10, 3 decimals)
print(f"{pi:e}")      # 3.141593e+00

Alignment and padding

SpecMeaningExampleOutput
{v:10}Pad to width 10, left-align stringsf"{'hi':10}|"hi |
{v:<10}Left-align explicitlyf"{'hi':<10}|"hi |
{v:>10}Right-alignf"{'hi':>10}|" hi|
{v:^10}Centref"{'hi':^10}|" hi |
{v:*^10}Centre, fill with *f"{'hi':*^10}|"****hi****|

Alignment is most useful when building fixed-width text tables for CLI output.

Number formatting

SpecMeaningExample (n=1234567.89)
:,.2fTwo decimals, thousands separator1,234,567.89
:.0fNo decimals (rounds)1234568
:+.1fAlways show sign+1234567.9
:010.2fZero-pad to width 101234567.89 (already 10)
:bBinaryf"{42:b}"101010
:xHex lowercasef"{255:x}"ff
:%Percentagef"{0.1752:.1%}"17.5%

Debug output with the = specifier

Python 3.8 added the = specifier, which prints the expression text alongside its value. It is invaluable for quick debugging without a debugger:

x = 42
y = x * 2 + 1
print(f"{x=}, {y=}")
# x=42, y=85

items = [1, 2, 3]
print(f"{len(items)=}")
# len(items)=3

You can combine = with a format spec: f"{pi=:.4f}" prints pi=3.1416.

Multiline f-strings

Triple-quoted f-strings span multiple lines naturally. This is useful for building structured text output like reports or SQL queries:

user = {"name": "Bob", "plan": "pro", "seats": 5}
msg = f"""
Account summary
---------------
Name  : {user['name']}
Plan  : {user['plan'].upper()}
Seats : {user['seats']:>3}
"""
print(msg.strip())
Avoid f-strings for SQL or shell commands Never build SQL queries or shell commands by interpolating user-supplied values into an f-string. Use parameterised queries and subprocess argument lists instead. F-strings perform no escaping.