Python 3 reference · String Methods

Python String format_map() Method

Like format(), but takes the values from a single mapping, which can be a dict or a dict subclass with defaults.

Python 3 Since 3.2

Syntax

str.format_map(mapping)

Parameters

ParameterDescription
mapping A dict (or any mapping) of field names to values.

Return value

A new string.

Examples

Fill from a dictTry it yourself
user = {"name": "Ada", "role": "admin"}
print("{name} ({role})".format_map(user))

Output

Ada (admin)
Defaults for missing keysTry it yourself
class Default(dict):
    def __missing__(self, key):
        return f"<{key}?>"

print("{name} from {city}".format_map(Default(name="Ada")))

Output

Ada from <city?>

Version history

VersionChange
Python 3.2Added