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

Python:如何使用转义字符解决“SyntaxError:行继续符后的意外字符”?

Python:如何使用转义字符解决“SyntaxError:行继续符后的意外字符”?

慕尼黑8549860 2021-09-14 20:37:58
我想做的事:我想打印以下消息作为输出:嗨“别名”您的电话号码是“1234567890”,您的电子邮件 ID 是“foo@bar.foo”Python代码name= "alias"phone= "1234567890"email="foo@bar.foo"print("Hi " + name + " your phone number is " + phone + " and your email ID is " + email)这给了我以下输出:嗨,别名您的电话号码是 1234567890,您的电子邮件 ID 是 foo@bar.foo但是这里缺少双引号 (" ")。我试图解决这个问题:print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
查看完整描述

3 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

用 str.format


前任:


name= "alias"

phone= "1234567890"

email="foo@bar.foo"


result = 'Hi "{}" your phone number is {} and your email ID is {}'

print(result.format(name, phone, email))

或f 字符串(py3.6)


result = f'Hi "{name}" your phone number is {phone} and your email ID is {email}'

print(result)


查看完整回答
反对 回复 2021-09-14
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

将打印行替换为: print("Hi " + "\""+ name +"\"" + " your phone number is " + "\""+ phone +"\""+ " and your email ID is " +"\""+ email+ "\"")


查看完整回答
反对 回复 2021-09-14
?
catspeake

TA贡献1111条经验 获得超0个赞

使用两种类型的引号:

print('Hi "' + name + '" your phone number is "' + phone + '" and your email ID is "' + email + '"')


查看完整回答
反对 回复 2021-09-14
  • 3 回答
  • 0 关注
  • 229 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号