Skip to content
String Methods

format()

Fill replacement fields in text with values.

str.format(*args, **kwargs)

str.format() performs string formatting operations by substituting positional and keyword arguments into replacement fields delimited by curly braces {}.

Parameters

NameDescription
*argsValues used by numbered or empty replacement fields.
**kwargsNamed values used by named replacement fields.

Returns

A new formatted string.

Try it

Run it and change it
template = "Hello, {}!"
print(template.format("Sam"))

Worth knowing

  • f-strings vs format(): In modern Python, f-strings (f'{var}') are generally preferred for inline readability, but str.format() remains essential for reusable template strings.

Related entries