Python String - format_map() Method
The Python format_map() method returns the formatted version of the given string where the specified values (provided from mapping) are inserted into the string's placeholder. The placeholder is defined using curly brackets: {}.
Syntax
string.format_map(mapping)
Parameters
mapping |
Required. Specify the dictionary containing mapping values to be inserted in the string. |
Return Value
Returns the formatted version of the given string.
Example:
In the example below, format_map() method returns the formatted version of the string called MyString.
mapping = {'name': 'John', 'age': 25} MyString = "I am {name} and I am {age} years old." print(MyString.format_map(mapping))
The output of the above code will be:
I am John and I am 25 years old.
Example:
The example below describes the concept of format_map() method in more depth.
mapping = {'name': ['John', 'Marry'], 'age': [25, 22]} MyString = "I am {name[0]} and I am {age[0]} years old." print(MyString.format_map(mapping)) MyString = "I am {name[1]} and I am {age[1]} years old." print(MyString.format_map(mapping))
The output of the above code will be:
I am John and I am 25 years old. I am Marry and I am 22 years old.
❮ Python String Methods