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:
>>> format(1234.5678, ",.2f")
'1,234.57'
format() Signature
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_specisn’t provided, thenformat()usually returns the same result as callingstr(value). Technically, Python translatesformat(value, format_spec)intotype(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:
>>> format(1234.5678, ".2f")
'1234.57'
With an integer number and a format specifier for thousand separators:
>>> format(1000000, ",d")
'1,000,000'
With a string and a format specifier for left alignment and filling:
>>> 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:
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:
>>> 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python's Format Mini-Language for Tidy Strings (Tutorial)
- String Interpolation in Python: Exploring Available Tools (Tutorial)
- Python's F-String for String Interpolation and Formatting (Tutorial)
- A Guide to Modern Python String Formatting Tools (Tutorial)
- Python String Formatting Tips & Best Practices (Course)
- Python String Formatting: Available Tools and Their Features (Quiz)
- Python's Format Mini-Language for Tidy Strings (Quiz)
- String Interpolation in Python: Exploring Available Tools (Quiz)
- Python 3's F-Strings: An Improved String Formatting Syntax (Course)
- Python's F-String for String Interpolation and Formatting (Quiz)
- Formatting Python Strings (Course)
- A Guide to Modern Python String Formatting Tools (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.