小弟在看《 Python 进阶》的时候,发现有这样一段事例代码:
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} == {1}".format(key, value))
其中,最后一句 print 有点看不太明白,{0} == {1}是什么意思呢? 请各位大神帮忙解释一下,多谢。
其中,最后一句 print 有点看不太明白,{0} == {1}是什么意思呢? 请各位大神帮忙解释一下,多谢。
|
co3siteJul 1, 2016
1
"{0} == {1}".format(key, value) -> str.format(key, value)
|
|
mgna17Jul 1, 2016
2
就是个替换呗,这里的 {0}, {1} 分别对应 format 中的参数
format(...) method of builtins.str instance S.format(*args, **kwargs) -> str Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}'). |
|
AllianzcortexJul 1, 2016
3
等价于 print'{} == {}'.format(key,value) 比 print '%s == %s' %(key,value) 要好
|
|
cloverstdJul 1, 2016
4
其实还有 print("{key} == {value}".format(key=key, value=value))
|
|
5
|
|
linuxchildJul 2, 2016 via Android
6
大括号和双等是分开的……说起来这个执行一下不就知道了吗
|