How to format list in python

Viewed 131

So I am trying to format a list for this:

['w', 'e', '5', 'p', '4', '7', '2', 'w']

to this:

we5p472w

Any idea how to do this?

NOTE: this is a LIST, not a STRING

3 Answers

Use the join() method:

l = ['w', 'e', '5', 'p', '4', '7', '2', 'w']

print (''.join(l))

Outut:

we5p472w

You can use any expression to join your list elements:

print (' '.join(l))

Output:

w e 5 p 4 7 2 w

Code:

myList = ['w', 'e', '5', 'p', '4', '7', '2', 'w']
myString = "".join(myList)
print(myString)

Output:

$ we5p472w
Related