在编写程序时,使用到了format(),但是出现’IndexError: tuple index out of range’问题。
    印象中在初始学习format函数时,前面的占位符是可以添加数字的,这一次并不允许。
    代码

    1. >>> a = 1
    2. >>> b = 2
    3. >>> c = 3
    4. >>> print('{1},{2},{3}'.format(a,b,c))
    5. Traceback (most recent call last):
    6. File "<pyshell#4>", line 1, in <module>
    7. print('{1},{2},{3}'.format(a,b,c))
    8. IndexError: tuple index out of range
    9. >>> print('{1}是{2}数{3}'.format(a, b, c))
    10. Traceback (most recent call last):
    11. File "<pyshell#5>", line 1, in <module>
    12. print('{1}是{2}数{3}'.format(a, b, c))
    13. IndexError: tuple index out of range

    调整后代码

    1. >>> a = 1
    2. >>> b = 2
    3. >>> c = 3
    4. >>> print('{},{},{}'.format(a,b,c))
    5. 1,2,3
    1. print('{0}是{1}数{2}'.format(a, b, c))
    2. 123

    注意从{0}开始