第二行那里不知道怎么错了
template = 'L{0} i{1} s{2},y{3} n{4} P{5}'
0 = 'ife'
1 = 's'
2 = 'hort'
3 = 'ou'
4 = 'eed'
5 = 'ython'
result = template.format(0 = ife , 1 = s , 2 = hort , 3 = ou , 4 = eed , 5 = ython)
print(result)
template = 'L{0} i{1} s{2},y{3} n{4} P{5}'
0 = 'ife'
1 = 's'
2 = 'hort'
3 = 'ou'
4 = 'eed'
5 = 'ython'
result = template.format(0 = ife , 1 = s , 2 = hort , 3 = ou , 4 = eed , 5 = ython)
print(result)
2021-09-02
变量不能以数字开头,所以'0''1'……这些数字不能作为变量,改成这样就可以了:
template = 'L{a0} i{a1} s{a2},y{a3} n{a4} P{a5}'
a0 = 'ife'
a1 = 's'
a2 = 'hort'
a3 = 'ou'
a4 = 'eed'
a5 = 'ython'
result = template.format(a0 = 'ife' , a1 = 's' , a2 = 'hort' , a3 = 'ou' , a4 = 'eed' , a5 = 'ython')
print(result)
或者不单独定义变量也可以:
template = 'L{0} i{1} s{2},y{3} n{4} P{5}'
result = template.format('ife','s','hort','ou','eed','ython')
print(result)
举报