Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python f-string format specifier: syntax and examples

How to use Python f-string format specifiers for alignment, numeric precision, grouping, and sign control, with practical examples.

~2 min readPython
#f-strings#format specifiers#string formatting#numeric formatting
Illust showing a Python f-string with a format specifier controlling alignment and numeric precision

The format specifier in a Python f-string controls how a value is converted to text. It appears after a colon inside the curly braces and can set width, alignment, numeric precision, grouping, and other presentation rules. This article explains the syntax and shows how to apply it to common formatting tasks.

The Format Specifier Syntax

An f-string is a string literal prefixed with f or F. Inside the braces, you write an expression followed by an optional format specifier:

python
value = 42 print(f"{value:>10}")

The format specifier follows the colon. Its general structure is:

[[fill]align][sign][#][0][width][grouping][.precision][type]

Each part is optional, and they must appear in this order:

  • fill and align control how the value occupies its width.
  • sign controls how positive and negative numbers are displayed.
  • # enables alternate form for some types.
  • 0 pads with zeros.
  • width is the minimum field width.
  • grouping adds separators.
  • .precision sets the number of digits after the decimal point.
  • type selects the presentation type, such as d, f, e, or x.

This structure lets you read and write format specifiers without memorizing each option separately.

Alignment, Width, and Fill

By default, a value is left-aligned within its width for strings and right-aligned for numbers. Use <, >, or ^ for left, right, and center alignment respectively. The fill character precedes the alignment character and is a space by default.

python
name = "Ada" print(f"{name:<10}|") print(f"{name:>10}|") print(f"{name:^10}|") print(f"{name:*^10}|")

For numeric values, the default alignment is right. For example, f"{value:>10}" right-aligns 42 in a field of width 10.

Sign and Zero Padding

For numeric types, + forces a sign on both positive and negative numbers, - shows a sign only for negative numbers (the default), and a space leaves a space for positive numbers. A leading 0 before the width pads the value with zeros after any sign.

python
print(f"{42:+d} {-42:+d}") print(f"{42:05d}")

Grouping and Precision

The grouping option uses , for decimal integers and floats, and _ is also available for some types:

python
print(f"{1234567:,d}")

The .precision option sets the number of digits after the decimal point for floating-point types, or the maximum number of characters for strings:

python
print(f"{3.14159:.2f}") print(f"{'Python':.3}")

Presentation Types

The type part selects the output format. Common choices are d for integers, f for fixed-point floats, e for scientific notation, and x for hexadecimal. You can combine these presentation types with the other options in a format specifier.

python
print(f"{255:d} {255:x}") print(f"{3.14159:e}")

The format specifier is a compact, readable way to control output without converting values manually.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.