Python:如何使用转义字符来解决;SyntaxError:行连续字符“后出现意外字符?”;?
我想做什么:Python:如何使用转义字符来解决;SyntaxError:行连续字符“后出现意外字符?”;?,python,string,escaping,string-concatenation,Python,String,Escaping,String Concatenation,我想做什么: print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \") name= "alias" phone= "1234567890" email="foo@bar.foo" result = 'Hi "{}" your phone number is {} and your email ID is {}' print(result.
print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
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))
我想将以下消息打印为输出:
您好“alias”您的电话号码是“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)
这为我提供了以下输出:
你好,alias您的电话号码是1234567890,电子邮件ID是
foo@bar.foo
但这里缺少双引号(“”)
我试图解决的问题:
print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
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))
使用
str.format
Ex:
print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
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-string(py3.6)
使用两种类型的引号:
print('Hi "' + name + '" your phone number is "' + phone + '" and your email ID is "' + email + '"')
将打印行替换为:
print(“Hi”+“\+”+“+”+“+”+“+”+“+”+”+”您的电话号码是“+”+“+”+”+”,您的电子邮件ID是“+”+“+”+”)
否,转义引号需要位于非转义引号内。但是,你为什么不用单引号来表示周围的内容呢?'Hi“{}”你的电话号码是“{}”,电子邮件ID是“{}”。格式(姓名、电话、电子邮件)
否决投票的原因是什么?我如何知道答案是否已经存在?我搜索了一下,但找不到解决方案。这个很好用!