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.

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:
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:
fillandaligncontrol how the value occupies its width.signcontrols how positive and negative numbers are displayed.#enables alternate form for some types.0pads with zeros.widthis the minimum field width.groupingadds separators..precisionsets the number of digits after the decimal point.typeselects the presentation type, such asd,f,e, orx.
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.
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.
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:
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:
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.
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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.