Python:语句print("\t",end='')中end=''的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27312273/
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
Python : meaning of end='' in the statement print("\t",end='')
提问by Rajath
This is the function for printing all values in a nested list (taken from Head first with Python).
这是用于打印嵌套列表中所有值的函数(取自 Head first with Python)。
def printall(the_list, level):
for x in the_list:
if isinstance(x, list):
printall(x, level=level + 1)
else:
for tab_stop in range(level):
print("\t", end='')
print(x)
The function is working properly.
该功能工作正常。
The function basically prints the values in a list and if there is a nested list then it print it by a tab space.
该函数基本上打印列表中的值,如果有嵌套列表,则通过制表符空间打印它。
Just for a better understanding, what does end=' '
do?
只是为了更好地理解,有什么作用end=' '
?
I am using Python 3.3.5
我正在使用 Python 3.3.5
For 2.7
对于 2.7
f = fi.input( files = 'test2.py', inplace = True, backup = '.bak')
for line in f:
if fi.lineno() == 4:
print line + '\n'
print 'extra line'
else:
print line + '\n'
as of 2.6 fileinput does not support with. This code appends 3 more lines and prints the appended text on the 3rd new line. and then appends a further 16 empty lines.
从 2.6 开始,fileinput 不支持 with。此代码再追加 3 行,并在第 3 个新行上打印追加的文本。然后再追加 16 行空行。
采纳答案by Bhargav Rao
The default value of end
is \n
meaning that after the print
statement it will print a new line. So simply stated end
is what you want to be printed after the print
statement has been executed
默认值end
是\n
这意味着后print
声明,将打印一个新行。所以简单地说end
就是你想在print
语句执行后打印什么
Eg: - print ("hello",end=" +")
will print hello +
例如: -print ("hello",end=" +")
将打印hello +