Skip to content

format()

The built-in format() function converts an input value into a formatted string representation based on the specified format. The function takes a format specification, format_spec, which determines how the value should be formatted:

Language: Python
>>> format(1234.5678, ",.2f")
'1,234.57'

format() Signature

Language: Python Syntax
format(value, format_spec="", /)

Arguments

Argument Description Default Value
value The value to be formatted. Required argument
format_spec A string specifying the format using the mini-language syntax. ""

Return Value

  • Returns a formatted string representation of the input value according to the specified format.
  • If format_spec isn’t provided, then format() usually returns the same result as calling str(value). Technically, Python translates format(value, format_spec) into type(value).__format__(value, format_spec), so a type that defines .__format__() separately from .__str__() can return something different.

format() Examples

With a floating-point number and a format specifier for two decimal places:

Language: Python
>>> format(1234.5678, ".2f")
'1234.57'

With an integer number and a format specifier for thousand separators:

Language: Python
>>> format(1000000, ",d")
'1,000,000'

With a string and a format specifier for left alignment and filling:

Language: Python
>>> format("Hello", "<20")
'Hello               '

To see how each part of a format specifier shapes the result, pick a value, then type a format_spec or click one of the examples from this page:

Interactive diagram — enable JavaScript to view.

The row of boxes splits your format_spec into its mini-language fields, such as fill, align, width, grouping, .precision, and type. Each field has its own color in the format() call above it. In the result, padding gets the same color as width, and separators get the same color as grouping. The len() of result readout counts the characters in the returned string.

For example, select "Hello" and change <20 to *^20 to fill both sides with asterisks. An invalid spec, like ,d with "Hello", shows the ValueError that Python raises instead of a result.

format() Common Use Cases

The most common use cases for the format() function include:

  • Formatting numbers for better readability, such as adding thousand separators or specifying decimal places.
  • Aligning text within a fixed width, using left, right, or center alignment.
  • Formatting dates and times in a specific format.
  • Preparing data for display in reports or user interfaces.

format() Real-World Example

Suppose you’re creating a financial report and need to format currency values consistently. Here’s how you can use the format() function to achieve that:

Language: Python
>>> sales = 12345678.9
>>> formatted_sales = format(sales, ",.2f")
>>> print(f"Total Sales: ${formatted_sales}")
Total Sales: $12,345,678.90

In this example, the format() function formats the sales amount with a comma as a thousand separator and two decimal places. Then, you add a dollar sign using an f-string. This helps present financial data clearly and professionally.

Python String Formatting: Available Tools and Their Features

Tutorial

Python String Formatting: Available Tools and Their Features

In this tutorial, you'll learn about the main tools for string formatting in Python, as well as their strengths and weaknesses. These tools include f-strings, the .format() method, and the modulo operator.

basics best-practices python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 21, 2026 • Reviewed by Dan Bader