语句print("\t",end='')中end=''的含义? [英] Meaning of end='' in the statement print("\t",end='')?

查看:95
本文介绍了语句print("\t",end='')中end=''的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于打印嵌套列表中所有值的函数(取自 Head first with Python).

This is the function for printing all values in a nested list (taken from 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 basically prints the values in a list and if there is a nested list then it print it by a tab space.

只是为了更好地理解,end=' ' 有什么作用?

Just for a better understanding, what does end=' ' do?

我使用的是 Python 3.3.5

I am using Python 3.3.5

对于 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'

从 2.6 开始,fileinput 不支持 with.此代码再追加 3 行,并在第 3 个新行上打印追加的文本.然后再追加 16 行空行.

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.

推荐答案

end 的默认值是 \n 意味着在 print 之后> 语句它将打印一个新行.所以简单地说 end 是你想要在 print 语句执行后打印的内容

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

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

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

这篇关于语句print("\t",end='')中end=''的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆