4

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

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:

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

Help would be much appreciated.

iruvar
  • 22,736
  • 7
  • 53
  • 82
dkentre
  • 289
  • 3
  • 5
  • 10
  • And now you have three upvotes :) – TerryA Sep 20 '13 at 01:34
  • It's always hard to read other people's minds, but I'm guessing they downvoted you because your question doesn't show any attempt to solve the problem yourself. I think that's unfair, because this isn't exactly an easy thing to search for. (Everything that seems like a good search turns up `repr`-vs.`-str` differences and other things that are just going to confuse you.) But some people are unfair and quick to downvote. In the future it might help to explain how you tried to search, and they'll be more sympathetic. Or you could just ignore them; it's not like the downvotes really hurt you… – abarnert Sep 20 '13 at 01:55
  • 1
    This is basically identical to one of your previous questions. – tacaswell Sep 20 '13 at 02:02
  • I guess that is what earned the -1s? – icedwater Sep 20 '13 at 02:15

2 Answers2

9

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

>>> print(*myList, sep='')
abc
abarnert
  • 354,177
  • 51
  • 601
  • 671
iruvar
  • 22,736
  • 7
  • 53
  • 82
8

Use str.join():

''.join(myList)

Example:

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

This joins every item listed separated by the string given.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • I could be wrong, but it looks like the OP wanted Python 3, so I added parens around your `print` and changed the docs link to 3.x. Otherwise, perfect answer. – abarnert Sep 20 '13 at 01:31
  • @abarnert Yep, thanks a lot. Title says 3.3.2 – TerryA Sep 20 '13 at 01:31
  • Funny how we both missed that. If it's not in the tag, we either assume 2.x, or try to figure it out heuristically from the text, without even looking at the giant bold letters. :) – abarnert Sep 20 '13 at 01:32
  • @abarnert Heh, I'm a 2.7 user, so I always assume 2.x :D (kinda bad that I do that, but meh :3) – TerryA Sep 20 '13 at 01:33