简体   繁体   English

为什么在Python的print语句末尾使用\\?

[英]Why \ is used at the end of print statement in python?

Consider the code: 考虑代码:

firstName = raw_input("Your fist Name: ")
lastName = raw_input("Your last Name: ")
print "Hello %s, May I call you" \
"Mr. %s?" % (firstName,lastName)

In line 3, what is the use of \\ ? 在第3行中, \\的用途是什么?

它允许您进行换行并在下一行继续您的语句

The \\ tells Python that the statement continues on the next line. \\告诉Python该语句在下一行继续。 So, the print statement doesn't end on the first line, it continues on the next. 因此, print语句不会在第一行结束,而在下一行继续。

print "Hello %s, May I call you" \
"Mr. %s?" % (firstName,lastName)

Is the same as: 是相同的:

print "Hello %s, May I call youMr. %s?" % (firstName,lastName)

The \\ character is used to break up long lines so they're easier to read and manage. \\字符用于分隔长行,因此更易于阅读和管理。 The third and fourth lines could have been written as a single line without the \\ instead: 第三和第四行可以写为没有\\一行:

print "Hello %s, May I call youMr. %s?" % (firstName,lastName)

(note there is no space between "you" and "Mr." in the original post either) (请注意,原始帖子中的“您”和“先生”之间也没有空格)

使您能够将一行字符串溢出到多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印语句中的end关键字在Python UBUNTU中不起作用 - End keyword in print statement is not working in Python UBUNTU 为什么python3的print语句在指定end关键字时不刷新output? - Why doesn't python3's print statement flush output when end keyword is specified? 除非使用print语句,为什么某些十六进制转义字符不会显示在python 3中? - Why are certain hexadecimal escape characters not displayed in python 3 unless the print statement is used? 为什么在python 2/3的同一行上不能使用for打印? - Why print cannot be used with for on the same line in python 2/3? 在 python 中,为什么此语句给出错误:- print(3 &lt; (2 or 10)) - In python why this statement is giving false:- print(3 < (2 or 10)) 为什么 Python 在这里打印 else 语句? - Why does Python print the else statement here? 我的python代码在循环结束时未运行打印语句 - My python code does not run a print statement at the end of a loop Python 打印语句在我的函数末尾不打印任何内容 - Python print statement not printing anything at the end of my function Python 打印语句上的“[WinError 233] 管道的另一端没有进程” - Python "[WinError 233] No process is on the other end of the pipe" on print statement 如何仅在 Python 中到达循环结束后打印失败语句? - How to print a fail statement only after reaching end of loop in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM