小弟在看《 Python 进阶》的时候,发现有这样一段事例代码:
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} == {1}".format(key, value))
其中,最后一句 print 有点看不太明白,{0} == {1}是什么意思呢? 请各位大神帮忙解释一下,多谢。
其中,最后一句 print 有点看不太明白,{0} == {1}是什么意思呢? 请各位大神帮忙解释一下,多谢。
1
co3site Jul 1, 2016
"{0} == {1}".format(key, value) -> str.format(key, value)
|
2
mgna17 Jul 1, 2016
就是个替换呗,这里的 {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 '}'). |
3
Allianzcortex Jul 1, 2016
等价于 print'{} == {}'.format(key,value) 比 print '%s == %s' %(key,value) 要好
|
4
cloverstd Jul 1, 2016
其实还有 print("{key} == {value}".format(key=key, value=value))
|
5
yexiaoxing Jul 1, 2016 via iPad |
6
linuxchild Jul 2, 2016 via Android
大括号和双等是分开的……说起来这个执行一下不就知道了吗
|