To format numbers with f-strings in Python, put a format specifier after a colon inside the braces: f"{price:,.2f}" gives 1,234.50 for 1234.5. The Python f-string format specifier follows one mini-language of fill, alignment, sign, width, grouping, precision and type. Learning the few common patterns covers almost every case: decimals, thousands separators, percentages, padding, hex, binary and scientific notation.
The examples run on Python 3.13 and 3.14 and use only the standard library. They also cover the z option added in Python 3.11, rounding behaviour with floats, and why you should use Decimal for money.
The full syntax of the spec after the colon is:
Every part is optional. The most useful ones are:
| Part | Values | Meaning |
|---|---|---|
| align | < > ^ = | Left, right, centre, or sign-aware padding |
| sign | + - or a space | Show sign always, only for negatives, or a space for positives |
| width | integer | Minimum total width |
| grouping | , or _ | Thousands separator |
| precision | .N | Digits after the point for floats |
| type | d f e g % x b o | How to present the number |
The last example pads on the left to a width of 15 so that numbers line up in a column.
Use .Nf for fixed-point numbers with N digits after the decimal point. The number is rounded for display only; the variable itself is unchanged.
Two surprises appear here. Values exactly halfway are rounded to the nearest even digit, so 2.5 becomes 2. And binary floats cannot store many decimal fractions exactly, so 2.675 is really slightly below the halfway point.
For values that must be exact, such as money, use decimal.Decimal. It supports the same specs.
Decimal rounds half-even by default as well, but because 19.995 is stored exactly, the result follows the decimal rules and not the float surprise.
Add , or _ for grouping, and + to always show the sign.
The 0 flag pads with zeros after the sign, up to the given total width.
Since Python 3.11 the z option converts negative zero after rounding to positive zero, which avoids output such as -0.00.
The % type multiplies by 100 and adds a percent sign. The e type uses scientific notation, and g picks the shorter of fixed or scientific form.
The g type removes trailing zeros and is useful for values with a wide range. Use .Nf when the output must always have the same number of decimals.
For integers, use x or X for hex, o for octal and b for binary. The # option adds the 0x, 0o or 0b prefix.
With # the width includes the prefix, so #010b gives ten characters in total. Grouping works for binary and hex too, in groups of four digits: f"{n:_b}" gives 1111_1111.
You can nest braces inside a spec so that the width or precision comes from variables.
The same spec works with format(value, spec) and str.format(), for example "{:,.2f}".format(n), so you can store a spec in a variable and reuse it.
Use :.2f, for example f"{value:.2f}". Integers must be converted first or use the f type directly; f"{3:.2f}" gives 3.00.
Add , before the precision: f"{n:,}" or f"{n:,.2f}". Use _ if you want underscores.
Use the % type and keep the original ratio: f"{0.256:.1%}" returns 25.6%. Do not multiply by 100 yourself.
Floats are stored in binary, so some decimal halves are not exact. Use Decimal when the exact decimal result matters, such as prices.
Format numbers by writing a spec after the colon: , for grouping, .Nf for fixed decimals, % for percentages, e and g for scientific forms, and x, o, b for other bases. Control layout with fill, alignment, sign, zero padding and width, and nest braces for dynamic values. Remember that floats round to nearest-even and carry binary error, so use Decimal for money.