[英]what is the 0 index in the print command when printing elements in a dictionary?
I've tried this piece of code but I don't understand why we use the 0 index in {0[a]}.我试过这段代码,但我不明白为什么我们在 {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
refers to the first argument passed to the format(...)
function. 0
是指传递给format(...)
function 的第一个参数。 1
is the second, and so on. 1
是第二个,以此类推。
Alternatively, you can pass name arguments to format
, and use their names in the string.或者,您可以将名称 arguments 传递给
format
,并在字符串中使用它们的名称。
0
is referring to the first argument of the str.format method. 0
指的是str.format方法的第一个参数。
Your print is thus equivalent to因此,您的 print 相当于
print('the first days is {0} , second days is {1}'.format(Day['a'], Day['b']))
or或者
print(f"the first days is {Day['a']} , second days is {Day['b']}")
0
is not for indexing the dictionary, it is for getting the first item in the .format(..)
parenthesis. 0
不是用于索引字典,而是用于获取.format(..)
括号中的第一项。
Example:例子:
>>> '{0} {1}'.format('Hello', 'World')
'Hello World'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.