如何在python中添加换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45377903/
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
How to add a line break in python?
提问by Sarad Sa.
I just made a program in python but the statements are too close to each other in the output. So how can i add a line break between two statements in python.
我刚刚在 python 中制作了一个程序,但输出中的语句彼此太接近。那么如何在python中的两个语句之间添加换行符。
回答by Simon Hobbs
You can print new line characters:
您可以打印换行符:
print('\n'*numlines)
回答by Johannes
\n
gives you a new line. You can put it anywhere in a string and when printing it you get a new line.
\n
给你一条新线。你可以把它放在字符串中的任何地方,打印时你会得到一个新行。
In [1]: print('ab')
ab
In [2]: print('a\nb')
a
b
There are more of this kind, including tabs etc. https://docs.python.org/3/reference/lexical_analysis.html#literals
还有更多这种类型,包括选项卡等。 https://docs.python.org/3/reference/lexical_analysis.html#literals
回答by papey
print(output1 + "\n")
print(output2)