Python 打印空行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13872049/
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
Print empty line?
提问by Gmenfan83
I am following a beginners tutorial on Python, there is a small exercise where I have to add an extra function call and print a line between verses, this works fine if I print an empty line in between function calls but if I add an empty print line to the end of my happyBirthday()I get an indent error, without the added print line all works fine though, any suggestions as to why?
我正在关注 Python 的初学者教程,有一个小练习,我必须添加一个额外的函数调用并在诗句之间打印一行,如果我在函数调用之间打印一个空行,这可以正常工作,但是如果我添加一个空打印行到我的末尾happyBirthday()我收到一个缩进错误,虽然没有添加打印行,但一切正常,有关为什么的任何建议?
Here is the code:
这是代码:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
print("\n") #error line
happyBirthday('Emily')
happyBirthday('Andre')
happyBirthday('Maria')
采纳答案by Bryan Oakley
You will always only get an indent error if there is actually an indent error. Double check that your final line is indented the same was as the other lines -- either with spaces or with tabs. Most likely, some of the lines had spaces (or tabs) and the other line had tabs (or spaces).
如果确实存在缩进错误,您将始终只收到缩进错误。仔细检查最后一行的缩进是否与其他行相同——无论是空格还是制表符。最有可能的是,某些行有空格(或制表符),而另一行有制表符(或空格)。
Trust in the error message -- if it says something specific, assume it to be true and figure out why.
相信错误消息——如果它说的是特定的东西,假设它是真的,并找出原因。
回答by adimoh
The two common to print a blank line in Python-
Python中打印空行的两种常见方式——
The old school way:
print "hello\n"Writing the word print alone would do that:
print "hello"print
老派的方式:
print "hello\n"单独写单词 print 会做到这一点:
print "hello"print
回答by user3743923
You can just do
你可以做
print()
to get an empty line.
得到一个空行。
回答by Singh Gaurav
Don't do
不要做
print("\n")
on the last line. It will give you 2 empty lines.
在最后一行。它会给你2个空行。
回答by rjkunde
Python 2.x:Prints a newline
Python 2.x:打印换行符
print
Python 3.x:You must call the function
Python 3.x:您必须调用该函数
print()
回答by Boris
Python's printfunction adds a newline character to its input. If you give it no input it will just print a newline character
Python 的print函数在其 input 中添加一个换行符。如果你没有输入它只会打印一个换行符
print()
Will print an empty line. If you want to have an extra line after some text you're printing, you can a newline to your text
将打印一个空行。如果您想在打印的某些文本之后多加一行,您可以为文本添加换行符
my_str = "hello world"
print(my_str + "\n")
If you're doing this a lot, you can also tell printto add 2 newlines instead of just one by changing the end=parameter (by default end="\n")
如果您print经常这样做,您还可以通过更改end=参数(默认情况下end="\n")告诉添加 2 个换行符而不是一个换行符
print("hello world", end="\n\n")
But you probably don't need this last method, the two before are much clearer.
但是你可能不需要最后一个方法,之前的两个更清楚。
回答by Murphy Kuffour
This is are other ways of printing empty lines in python
这是在 python 中打印空行的其他方法
# using \n after the string creates an empty line after this string is passed to the the terminal.
print("We need to put about", average_passengers_per_car, "in each car. \n")
print("\n") #prints 2 empty lines
print() #prints 1 empty line

