str.format
Insert values into a string template in place of the fields in braces.
Python 3.13
str.format(*args, **kwargs)Parameters
- *argsAny
- Values for the fields {} and {0}, {1}… in order.
- **kwargsAny
- Values for named fields such as {name}.
Return value
A new string with the values inserted.
Examples
Inserting a value into a template
Python 3.13
print('Hello, {}'.format('world'))Hello, world
Named fields
Python 3.13
print('{name} is {age}'.format(name='Anna', age=25))Anna is 25
Two decimal places
Python 3.13
print('{:.2f}'.format(3.14159))3.14
Details
Modern code more often uses f-strings: f"Hello, {name}". The value format goes after a colon: "{:.2f}" keeps two decimal places.
