Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个Python脚本在多行字符串前面有一个\以及它的作用是什么?_Python_String_Multiline - Fatal编程技术网

为什么这个Python脚本在多行字符串前面有一个\以及它的作用是什么?

为什么这个Python脚本在多行字符串前面有一个\以及它的作用是什么?,python,string,multiline,Python,String,Multiline,在Python脚本中,我看到字符串前面有一个\: print """\ Content-Type: text/html\n <html><body> <p>The submited name was "%s"</p> </body></html> """ % name print”“”\ 内容类型:text/html\n 提交的名称为“%s” “”%name 如果我删除\它将断开。它做什么?它告诉Python忽略反斜杠后

在Python脚本中,我看到字符串前面有一个\:

print """\
Content-Type: text/html\n
<html><body>
<p>The submited name was "%s"</p>
</body></html>
""" % name
print”“”\
内容类型:text/html\n
提交的名称为“%s”

“”%name

如果我删除\它将断开。它做什么?

它告诉Python忽略反斜杠后面的换行符。结果字符串以
内容类型开头:
,而不是以
\n内容类型开头:

>>> '''\
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'
注意这两个结果的不同之处;第一个在开头没有换行符,另一个在开头没有换行符