Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.format

Substitutes positional or named fields and applies each field’s format specification.

Version: Python 3.14; repository image, production runtime not attested

Syntax

template.format(*args, **kwargs)

Substitutes positional or named fields and applies each field’s format specification.

Double literal braces when they must appear around a formatted value.

The named field name reads A, while 03d displays the integer seven as 007. In the second template doubled outer braces enclose one positional field, so the literal braces remain around the substituted number.

Examples

print("{name}: {n:03d}".format(name="A", n=7))
print("{{{}}}".format(7))

Result

A: 007
{7}

The named field name reads A, while 03d displays the integer seven as 007. In the second template doubled outer braces enclose one positional field, so the literal braces remain around the substituted number.

Sources