format(value, format_spec='')

Formats the specified value.

Parameters

The format() function takes two parameters:

  • value - value to be formatted
  • format_spec - format settings for formatting. The interpretation depends on the type of the value. By default, it is an empty string, usually having the same effect as applying str().

The interpretation of the format settings depends on the type of the passed value, but most built-in types use a common mini-language for formatting.

Calling format(value, format_spec) is equivalent to type(value).format(value, format_spec), without considering the instance dictionary when searching for the format() method.

Return Value

  • Returns a formatted representation of the given value, specified by the format specifier.

Examples

format(5)  # '5'
format(5, '.2f')  # '5.00'
format(5, '+^8.2f')  # '++5.00++'