Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

format

format() converts a value to a formatted string representation. Its optional format specification controls presentation according to the value’s type.

How it works

format() converts a value to its formatted string representation. The optional format specification is interpreted by the value’s type, while many built-in types use the standard formatting syntax.

Syntax

format(value, format_spec='', /)

Parameters

NameTypeDescription
valueobjectThe object whose formatted representation is requested.
format_specstrAn optional type-dependent formatting specification; it defaults to an empty string.

Returns

A string containing the formatted representation of value.

Key points

  • The function converts one value into its formatted representation using an optional specification.
  • The signature accepts the value and an optional format specification as positional-only arguments.
  • Most built-in types understand the shared standard formatting syntax.
  • Formatting behavior is selected according to the value’s type.

Common mistakes

  • A format specification is type-dependent, so the same specification may not work consistently across unrelated value types.

Example: Format values with type-specific specifications

price = 12.5
print(format(price, ".2f"))
print(format(255, "#x"))

Formats a floating-point value to two decimal places and an integer as a prefixed hexadecimal string.

When to use it

Present numbers

Display numeric output with controlled precision or notation.

Render alternate representations

Produce identifiers or values in bases such as hexadecimal.

Format custom objects

Delegate presentation to an object’s type-specific formatting rules.

Interview questions

QWhat does format() do?

format() returns a formatted representation of value as a string. Its optional format_spec controls the presentation, and the value’s type determines how that specification is interpreted.

Follow-up: What happens when format_spec is omitted? It defaults to an empty string, so the type supplies its default representation.

QHow does format_spec affect the result?

The first argument is the value to format, and the second optional argument is the format specification. The specification is interpreted according to the value’s type.

Follow-up: Why can a specification differ between types? Built-in types may implement different formatting rules even though they share the standard formatting syntax.

QWhy might code use format() instead of changing the value?

format() is a built-in function, so it is available without importing a module. It returns the formatted representation rather than modifying the original value.

Follow-up: How can formatting be embedded in larger text? Use a formatted string literal or another string-formatting mechanism when surrounding text is needed.

  • str
  • __format__
  • Formatted string literals

Source: Built-in Functions. Checked 2026-10-16.