Python 3.2+Strings (str)function
str.format_map
formatting
Like format, but take substitutions directly from a mapping (no copy).
Syntax
s.format_map(mapping)
The point is not just that the mapping is not copied: format_map accepts any mapping, so you can pass a collections.defaultdict or your own class with __missing__ and have absent keys fall back to a placeholder instead of blowing up. With a plain dict a missing key still raises KeyError.
Examples
print('{x}!'.format_map({'x': 'ok'})) # → ok!
print('{a}-{b}'.format_map({'a': 1, 'b': 2})) # → 1-2
print('{name}'.format_map({'name': 'Аня'})) # → Аня
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy