[英]Python is printing when it should not print, and printing what should not print
[英]what is the 0 index in the print command when printing elements in a dictionary?
我试过这段代码,但我不明白为什么我们在 {0[a]} 中使用 0 索引。
Day={'a':'Saturday','b':'Sunday','c':'Monday',
'd':'Tuesday','e':'Wednesday','f':'Thursday','g':'Friday'}
print('the first days is {0[a]} , second days is {0[b]}'.format(Day))
0
是指传递给format(...)
function 的第一个参数。 1
是第二个,以此类推。
或者,您可以将名称 arguments 传递给format
,并在字符串中使用它们的名称。
0
指的是str.format方法的第一个参数。
因此,您的 print 相当于
print('the first days is {0} , second days is {1}'.format(Day['a'], Day['b']))
或者
print(f"the first days is {Day['a']} , second days is {Day['b']}")
0
不是用于索引字典,而是用于获取.format(..)
括号中的第一项。
例子:
>>> '{0} {1}'.format('Hello', 'World')
'Hello World'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.