原文链接:http://www.juzicode.com/archives/2725
格式化字符串时提示index超出范围:IndexError: Replacement index 5 out of range for positional args tuple
#juzicode.com/vx:桔子code
a = 3
b = 5.12345
c = '桔子code'
d = [1,2,3,4,5]
e = (1,2,3,4,5)
f = {1,2,3,4,5}
out = 'a:{0},b:{1},c:{2},d:{3},e:{4},f:{5}'.format(a,b,c,d,e)
print('字符串转换后',out)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
in
7 f = {1,2,3,4,5}
8
----> 9 out = 'a:{0},b:{1},c:{2},d:{3},e:{4},f:{5}'.format(a,b,c,d,e)
10 print('字符串转换后',out)
IndexError: Replacement index 5 out of range for positional args tuple
1、格式化方法提供了0~5的6个占位符,但是format()内只有5个变量。
1、增加占位符为5的变量,在format()中增加变量f:
#juzicode.com/vx:桔子code
a = 3
b = 5.12345
c = '桔子code'
d = [1,2,3,4,5]
e = (1,2,3,4,5)
f = {1,2,3,4,5}
out = 'a:{0},b:{1},c:{2},d:{3},e:{4},f:{5}'.format(a,b,c,d,e,f)
print('字符串转换后',out)