Home >>Python String Methods >Python String format_map() method

Python String format_map() method

Python String format_map() method

Python string format_map() method is employed to return a dictionary key’s value. It is almost like the format() method but the difference is that the format() method creates a new dictionary whereas the format_map() doesn't.

Syntax:
string.format_map(input_dict)

Parameter Values

Parameter Description
input_dict It defines the variable in which the input dictionary is stored.
Here is an example of Python format_map() method:

a = {'x':'Jerry', 'y':'Stark'} 
print("{x}'s last name is {y}".format_map(a))

Output:
Jerry's last name is Stark
Example 2:

# Input dictionary 
profession = { 'name':['Barry', 'Bruce'], 
               'profession':['Flash', 'Batman'] } 
                       
# Use of format_map() function  
print('{name[0]} is the {profession[0]}. '
      .format_map(profession)) 
        
print('{name[1]} is the {profession[1]}. '
      .format_map(profession)) 

Output:
Barry is the Flash.
Bruce is the Batman.

No Sidebar ads