Built-in Functions and Types

format()

Produce a formatted string using the value type’s __format__() implementation. The standard mini-language supports options such as alignment, precision and numeric bases, but each type defines which specifications it accepts. An empty specification usually behaves like str(value). This function formats one value; str.format() instead processes a template. Unsupported specifications may raise ValueError or TypeError.

Formal Syntax & Structure

python syntaxpython
format(value, format_spec='', /)

Parameters & Attributes

NameTypeDescription
valueobjectValue to format.
format_specstrType-dependent formatting instruction; default empty.

Return Value

str

Formatted text; does not change the input.

Working Example

Example Snippetpython
print(format(27, "04d"))print(format(0.375, ".1%"))print(format("ID", "*>6"))
Python Reference