简体   繁体   中英

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 \\ ?

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

The \\ tells Python that the statement continues on the next line. So, the print statement doesn't end on the first line, it continues on the next.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM