在编写程序时,使用到了format(),但是出现’IndexError: tuple index out of range’问题。
印象中在初始学习format函数时,前面的占位符是可以添加数字的,这一次并不允许。
代码
>>> a = 1
>>> b = 2
>>> c = 3
>>> print('{1},{2},{3}'.format(a,b,c))
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print('{1},{2},{3}'.format(a,b,c))
IndexError: tuple index out of range
>>> print('{1}是{2}数{3}'.format(a, b, c))
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print('{1}是{2}数{3}'.format(a, b, c))
IndexError: tuple index out of range
调整后代码
>>> a = 1
>>> b = 2
>>> c = 3
>>> print('{},{},{}'.format(a,b,c))
1,2,3
print('{0}是{1}数{2}'.format(a, b, c))
1是2数3
注意从{0}开始