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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:38:28  来源:igfitidea点击:

Python : meaning of end='' in the statement print("\t",end='')

pythonpython-3.xpython-3.3

提问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 endis \nmeaning that after the printstatement it will print a new line. So simply stated endis what you want to be printed after the printstatement has been executed

默认值end\n这意味着后print声明,将打印一个新行。所以简单地说end就是你想在print语句执行后打印什么

Eg: - print ("hello",end=" +")will print hello +

例如: -print ("hello",end=" +")将打印hello +

回答by RemcoGerlich

See the documentation for the print function: print()

请参阅打印功能的文档:print()

The content of endis printed after the thing you want to print. By default it contains a newline ("\n") but it can be changed to something else, like an empty string.

的内容end打印在您要打印的东西之后。默认情况下,它包含一个换行符 ( "\n"),但可以将其更改为其他内容,例如空字符串。