为了账号安全,请及时绑定邮箱和手机立即绑定

第7句哪里错了是什么问题,想法思路错了吗

# 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)#


正在回答

1 回答

我举个栗子,你看能不能发现你原本的错误:

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中”{ }”里的名“=”要插入的字符串所对应的变量名

如果“{ }”中的名和变量名相同,就会冲突了

2 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python3 入门教程(新版)
  • 参与学习       147066    人
  • 解答问题       1049    个

python3入门教程,让你快速入门并能编写简单的Python程序

进入课程

第7句哪里错了是什么问题,想法思路错了吗

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信