如何在没有显示括号和输出中的“”的情况下打印列表? Python 3.3.2 [重复]

时间:2022-06-07 02:27:17

This question already has an answer here:

这个问题在这里已有答案:

So say I have a list named myList and it looks something like this :

所以说我有一个名为myList的列表,它看起来像这样:

myList = ["a", "b", "c"]

How would you print it to the screen so it prints :

如何将其打印到屏幕上以便打印:

abc 

(yes, no space inbetween)

(是的,中间没有空格)

If I use print(myList) It prints the following:

如果我使用print(myList)它打印以下内容:

['a', 'b', 'c']

Help would be much appreciated.

非常感谢帮助。

2 个解决方案

#1


8  

With Python 3, you can pass a separator to print. * in front of myList causes myList to be unpacked into items:

使用Python 3,您可以传递分隔符进行打印。 *在myList前面导致myList被解压缩到项目中:

>>> print(*myList, sep='')
abc

#2


6  

Use str.join():

使用str.join():

''.join(myList)

Example:

例:

>>> myList = ["a", "b", "c"]
>>> print(''.join(myList))
abc

This joins every item listed separated by the string given.

这将连接由给定字符串分隔的每个项目。

#1


8  

With Python 3, you can pass a separator to print. * in front of myList causes myList to be unpacked into items:

使用Python 3,您可以传递分隔符进行打印。 *在myList前面导致myList被解压缩到项目中:

>>> print(*myList, sep='')
abc

#2


6  

Use str.join():

使用str.join():

''.join(myList)

Example:

例:

>>> myList = ["a", "b", "c"]
>>> print(''.join(myList))
abc

This joins every item listed separated by the string given.

这将连接由给定字符串分隔的每个项目。