Built-in function

format()

Turns a value into nicely formatted text with a format spec: decimals, thousands separators, percentages, padding, alignment or binary.

BeginnerEvery example below runs in your browser. Change the code and press Run.

format(value, format_spec='')

format() converts one value into a string, following a short recipe called the format spec. format(3.14159, ".2f") gives '3.14'; format(1234567, ",") gives '1,234,567'.

It uses the same mini-language as f-strings: the part after the colon in f"{price:.2f}" is a format spec. Learning it once helps with format(), f-strings and str.format() alike.

The most useful specs: .2f (two decimals), , (thousands separator), % (percentage), >10 / <10 / ^10 (right, left or centre in 10 characters), 05 (pad with zeros), and b, x, o for binary, hex and octal.

format() always returns a string. To do more maths, keep the number and only format it when you print it.

Parameters

ParameterWhat it is
value
required
The value to format: a number, a string, a date, or any object that supports formatting.
format_spec
optional, default ''
The recipe, such as ".2f", ",", ">8" or "08b". Empty means the same as str(value).

Return value

A string.

Examples

1Decimals and thousands separators

Pythoneditable
price = 1234.5
print(format(price, ".2f"))
print(format(price, ",.2f"))
print(format(9876543, ","))
Output
1234.50
1,234.50
9,876,543

2Percentages

Pythoneditable
share = 0.2567
print(format(share, ".0%"))
print(format(share, ".1%"))
Output
26%
25.7%

% multiplies by 100 and adds the percent sign.

3Line up a table with alignment and width

<14 puts the text on the left of a 14-character space; >6.2f puts the number on the right of 6 characters.

Pythoneditable
rows = [("Coffee", 3.2), ("Croissant", 2.75), ("Orange juice", 4.5)]
for name, cost in rows:
    print(format(name, "<14") + format(cost, ">6.2f"))
Output
Coffee          3.20
Croissant       2.75
Orange juice    4.50

4Zero padding, binary and hex

Pythoneditable
print(format(7, "03d"))
print(format(10, "b"))
print(format(10, "08b"))
print(format(255, "x"))
print(format(255, "#X"))
Output
007
1010
00001010
ff
0XFF

5Format a date

Dates bring their own format codes, such as %d for the day and %B for the month name.

Pythoneditable
from datetime import date

day = date(2026, 3, 14)
print(format(day, "%d/%m/%Y"))
print(format(day, "%A %d %B"))
Output
14/03/2026
Saturday 14 March

Common mistakes

Doing maths with the formatted result

format() returns text. Adding two formatted prices joins the strings instead of adding the numbers. Keep the numbers, and format only for display.

Pythoneditable
a = format(2.5, ".2f")
b = format(1.25, ".2f")
print(a + b)
print(format(2.5 + 1.25, ".2f"))
Output
2.501.25
3.75

Using a number spec on a string

format("42", ".2f") raises ValueError, because "42" is text. Convert it with float() first.

Pythoneditable
value = "42"
try:
    print(format(value, ".2f"))
except ValueError:
    print("ValueError: .2f needs a number")
print(format(float(value), ".2f"))
Output
ValueError: .2f needs a number
42.00

Questions people ask

What is the difference between format() and str.format()?

format(value, spec) formats one value. str.format() fills several {} places inside a template: "{} costs {:.2f}".format(name, price). Both use the same specs.

Should I use format() or f-strings?

In new code, f-strings are usually easiest: f"{price:,.2f}". format() is handy when the spec is stored in a variable or when you pass formatting as a function.

How do I round to 2 decimal places for display?

format(x, ".2f") or f"{x:.2f}". It rounds for display only; the number itself does not change. To get a rounded number, use round().

How do I use a dot as the thousands separator, as in Europe?

Format with , and swap the characters: format(1234.5, ",.2f").replace(",", "X").replace(".", ",").replace("X", ".") gives '1.234,50'.

Spotted a mistake on this page? Tell us