Why does this Python script have a \ before the multi-line string and what does it do?

multiline, python, string

Solution

It tells Python to ignore the newline directly following the backslash. The resulting string starts with `Content-Type:`, not with `\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'

Note how the two results differ; the first has no `\n` newline character at the start, the other does.

Problem

In a Python script I'm looking at the string has a \ before it: ``` print """\ Content-Type: text/html\n <html><body> <p>The submited name was "%s"</p> </body></html> """ % name ``` If I remove the \ it breaks. What does it do?

Original source