# Enter a code
template = 'Life {0} ,you {1}.'
result = template.format('is short','need Python')
print(result) #
template ='Life {a},you{b}'
a = 'is short'
b ='need Python'
result = template.format(a = is short,b = need Python)
print(result)#
我举个栗子,你看能不能发现你原本的错误:
temple='I am{first},as well as{second}' a='smart' b='handsome'
如果我这里要输出‘I am smart,as well as handsome’
我应该这样写:
result=temple.format(first=a,second=b)
可以看出,format中的写法应该是使temple中”{ }”里的名“=”要插入的字符串所对应的变量名
如果“{ }”中的名和变量名相同,就会冲突了