Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何像字符串一样打印列表?_Python_List_Printing - Fatal编程技术网

Python 如何像字符串一样打印列表?

Python 如何像字符串一样打印列表?,python,list,printing,Python,List,Printing,我正在尝试像字符串一样打印列表: list = ['a', 'b', 'c', 'd', 'e', 'f'] 我想要的输出 abcdef 您应该使用join方法,在python中,join返回一个字符串,其中序列的字符串元素已通过str分隔符连接 str = '' sequence = ['a', 'b', 'c', 'd', 'e', 'f'] print str.join(sequence) 您可以按顺序传递任何列表或元组。如果您只想打印它,可以使用print的end参

我正在尝试像字符串一样打印列表:

    list = ['a', 'b', 'c', 'd', 'e', 'f']
我想要的输出

    abcdef
您应该使用join方法,在python中,join返回一个字符串,其中序列的字符串元素已通过str分隔符连接

str = ''
sequence = ['a', 'b', 'c', 'd', 'e', 'f']
print str.join(sequence)

您可以按顺序传递任何列表或元组。

如果您只想打印它,可以使用print的end参数。它默认为\n,并且是在传递给它的字符串末尾打印的内容:

for i in list:
    print(i, end="")
如果您确实需要该字符串,可以将它们一起添加(不是在所有python版本中):

string=""
for i in list:
    sting+=i
如果您知道列表中有多少项:

string=list[0]+list[1]+list[2]+list[3]+list[4]
或:


sep是打印在每个字符串之间的内容,默认为,但是上面两个内容非常混乱。

使用.joinlist…您应该自己尝试一些东西,如果遇到问题,您可以返回这些内容,以替代SpiXel的答案,这也是非常有效的,并且更易于使用。
print(list[0],list[1],list[2],list[3],list[4], sep="")