format method

The string method that returns a copy of a string with each {} replacement field filled in from its arguments.

In more detail

"Hello, {}!".format(name) substitutes name for the braces and converts it to text for you. Fields can be numbered, {0}, or named, {animal}, and a literal brace is written doubled, {{ or }}. The newer f-strings do the same thing more briefly.

Examples

"{} + {} = {}".format(2, 3, 5)
Because format can walk attributes, a format string chosen by a caller is a small program, which is the template injection in the course.

Related terms

Taught in

References