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
| Parameter | Description |
|---|---|
mapping |
A dict (or any mapping) of field names to values. |
Return value
A new string.
Examples
user = {"name": "Ada", "role": "admin"}
print("{name} ({role})".format_map(user))
Output
Ada (admin)
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
| Version | Change |
|---|---|
| Python 3.2 | Added |
Related
str.format()· Return a formatted string, filling {} placeholders with the arguments. f-strings are usually simpler.- Python String Formatting · tutorial