Why does this Python script have \ in front of a multi-line string and what does it do?

In a Python script, I look at the line: \ in front of it:

print """\ Content-Type: text/html\n <html><body> <p>The submited name was "%s"</p> </body></html> """ % name 

If I remove \ it breaks. What is he doing?

+6
source share
1 answer

It tells Python to ignore the newline immediately after the backslash. The resulting string starts with Content-Type: not \nContent-Type: ::

 >>> '''\ This is the first line. This is the second line. ''' 'This is the first line.\nThis is the second line.\n' >>> ''' ... This is the first line. ... This is the second line. ... ''' '\nThis is the first line.\nThis is the second line.\n' 

Notice how the two results differ; the first does not have a newline \n at the beginning, and the other does.

+8
source

Source: https://habr.com/ru/post/958878/


All Articles