Python字符串格式_map()方法

Python字符串 格式_map() 方法是Python中的一个内置函数,用于返回字典键的值。

null

语法:

一串格式图(z)

参数:

这里z是一个变量,输入字典存储在其中,字符串是输入字典的键。input_dict:接受一个参数,即输入字典。

返回:

返回输入字典的键值。

示例1:Python字符串格式_map()方法

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))


输出:

John's last name is Wick

例2:

Python3

# input stored in variable a.
a = { 'x' : "geeksforgeeks" , 'y' : 'b' }
# Use of format_map() function
print ( '{x} {y}' .format_map(a))


输出:

geeksforgeeks b

例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))


输出:

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

例4: 实际的 应用

format_map()函数可用于任何实际应用。

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 )


输出:

George has 10 new messages
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享