Twhite1195
Twhite1195

Reputation: 351

list with numbers and strings to single string python

How do I print a list that has numbers and strings to a single string? For example, I have this list: ["(",3,"+",4,"-",3,")"], I would like it to be printed as :(3+4-4). I tried to use the join command, but I keep having issues with the numbers.

Upvotes: 1

Views: 494

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You have to cast the ints to str, str.join expects strings:

l = ["(",3,"+",4,"-",3,")"]

print("".join(map(str,l)))
(3+4-3)

Which is equivalent to:

print("".join([str(x) for x in l]))

To delimit each element with a space use:

print(" ".join(map(str,l)))

Upvotes: 2

Related Questions