0

I am trying to print a list like a string:

    list = ['a', 'b', 'c', 'd', 'e', 'f']

the output I want it to be;

    abcdef
explorer
  • 31
  • 1
  • 6

2 Answers2

2

You should use the join() method, in python, join() returns a string in which the string elements of sequence have been joined by str separator.

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

You can pass any list or tuple as sequence.

moghya
  • 854
  • 9
  • 18
SpiXel
  • 4,338
  • 1
  • 29
  • 45
0

If you just want to print it, you can make use of the end parameter of print. It defaults to "\n" and is what is printed at the end of the string passed to it:

for i in list:
    print(i, end="")

If you actually want the string, you can just add them together(not in all python versions):

string=""
for i in list:
    sting+=i

If you know how many items are in your list:

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

or:

print(list[0],list[1],list[2],list[3],list[4], sep="")

(sep is what is printed between each string, defaults to " ", However the two above are quite messy.

Artemis
  • 2,553
  • 7
  • 21
  • 36
  • These are all alternatives to SpiXel's answer, which is also perfectly valid, and is easier to use. – Artemis Mar 24 '18 at 13:58