Python provides several ways to format strings, but the str.format() method, introduced in Python 2.6, remains one of the most versatile and powerful tools in a developer's arsenal. While f-strings (introduced in Python 3.6) are often preferred for their concise syntax, str.format() excels in scenarios involving dynamic templates, internationalization, and externalized configuration where the format string is decoupled from the data.

The str.format() method uses curly braces {} as placeholders within a string literal. These placeholders are replaced by the arguments passed to the method. This approach offers significant advantages over the older % formatting style, including better support for object attributes and improved readability.

Basic Syntax and Placeholder Logic

At its simplest level, str.format() performs basic substitution. The number of curly braces in the string usually matches the number of arguments passed to the method.

Using Default Placeholders

When you use empty curly braces, Python maps the arguments to the placeholders based on their position. This is the most common usage for simple string assembly.