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
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
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
.
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.