Python: How to use Escape character to solve "SyntaxError: unexpected character after line continuation character"?
Python: How to use Escape character to solve "SyntaxError: unexpected character after line continuation character"?
我想做的事情:
我想打印以下消息作为输出:
Hi "alias" your phone number is "1234567890" and your email ID is
"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)
这给了我以下输出:
Hi alias your phone number is 1234567890 and your email ID is
foo@bar.foo
但是这里缺少双引号 (" ")。
我试图解决的问题:
print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
使用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-string(py3.6)
result = f'Hi "{name}" your phone number is {phone} and your email ID is {email}'
print(result)
使用两种类型的引号:
print('Hi "' + name + '" your phone number is "' + phone + '" and your email ID is "' + email + '"')
将打印行替换为:
print("Hi " + "\""+ name +"\"" + " your phone number is " + "\""+ phone +"\""+ " and your email ID is " +"\""+ email+ "\"")
我想做的事情:
我想打印以下消息作为输出:
Hi "alias" your phone number is "1234567890" and your email ID is "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)
这给了我以下输出:
Hi alias your phone number is 1234567890 and your email ID is foo@bar.foo
但是这里缺少双引号 (" ")。
我试图解决的问题:
print("Hi " + \" name \" + " your phone number is " + \" phone \"+ " and your email ID is " + \" email \")
使用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-string(py3.6)
result = f'Hi "{name}" your phone number is {phone} and your email ID is {email}'
print(result)
使用两种类型的引号:
print('Hi "' + name + '" your phone number is "' + phone + '" and your email ID is "' + email + '"')
将打印行替换为:
print("Hi " + "\""+ name +"\"" + " your phone number is " + "\""+ phone +"\""+ " and your email ID is " +"\""+ email+ "\"")