Open In App

Python String format_map() Method

Last Updated : 05 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Python String format_map() method is an inbuilt function in Python, which is used to return a dictionary key's value.

Syntax: 

string.format_map(z)

Parameters: 

Here z is a variable in which the input dictionary is stored and string is the key of the input dictionary. input_dict: Takes a single parameter which is the input dictionary.

Returns: 

Returns key's values of the input dictionary.

Example 1: Python String format_map() method

Python3
# input stored in variable a.
a = {'x':'John', 'y':'Wick'}

# Use of format_map() function
print("{x}'s last name is {y}".format_map(a))

Output: 

John's last name is Wick

Example 2:

Python3
# input stored in variable a.
a = {'x':"geeksforgeeks", 'y':'b'}

# Use of format_map() function
print('{x} {y}'.format_map(a))

Output: 

geeksforgeeks b

Example 3:

Python3
# Input dictionary
profession = { 'name':['Barry', 'Bruce'],
               'profession':['Engineer', 'Doctor'],
               'age':[30, 31] }
                     
# Use of format_map() function 
print('{name[0]} is an {profession[0]} and he'
      ' is {age[0]} years old.'.format_map(profession))
      
print('{name[1]} is an {profession[1]} and he'
      ' is {age[1]} years old.'.format_map(profession))

Output: 

Barry is an Engineer and he is 30 years old.
Bruce is an Doctor and he is 31 years old.

Example 4: Practical Application

The format_map() function can be used in any practical application.

Python3
# Python code showing practical 
# use of format_map() function
def chk_msg(n):
        
    # input stored in variable a.
    a = {'name':"George", 'mesg':n}
    
    # use of format_map() function 
    print('{name} has {mesg} new messages'.format_map(a))

chk_msg(10)

Output: 

George has 10 new messages

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy