python 打印空行有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2575584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does printing an empty line do?
提问by brilliant
I know this question may well be the silliest question you've heard today, but to me it is a big question at this stage of my programming learning.
我知道这个问题很可能是你今天听到的最愚蠢的问题,但对我来说,在我编程学习的这个阶段,这是一个大问题。
Why is the second empty line needed in this Python code? What does that line do?
为什么此 Python 代码中需要第二个空行?那条线有什么作用?
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
回答by Ned Batchelder
It prints an empty line, just as you have said. It will leave a blank line in the output. The print statement prints its arguments, and then a newline, so this prints just a newline.
正如您所说,它会打印一个空行。它会在输出中留下一个空行。打印语句打印它的参数,然后是一个换行符,所以这只是打印一个换行符。
You could accomplish the same thing with just:
你可以完成同样的事情:
print
回答by Ignacio Vazquez-Abrams
A blank line is required between the headers and the body in an HTTP response, so a CGI script will print a blank line at just that spot. There's no need for the quotes though, since an unadorned print
will output a blank line.
在 HTTP 响应中的头和正文之间需要一个空行,因此 CGI 脚本将在该位置打印一个空行。但是不需要引号,因为未经修饰的print
将输出一个空行。
回答by confiq
Did you even tried to telnet web-server? It needs \n. So, it's what it does, you can write it in one line if needed.
你有没有尝试 telnet web-server?它需要\n。所以,这就是它的作用,如果需要,您可以将其写在一行中。
print 'Content-Type: text/plain\n\nHello World'