+ 1

How to concatenate str & list

Eg " this costs" + str(11+56) + " Dollars " + "for" +"Bob:Alice:Tom".split(":")

18th Aug 2019, 5:03 PM
Smith Gonsalves
Smith Gonsalves - avatar
3 odpowiedzi
+ 3
As Diego demonstrated, you can only concatenate str to str, so you must convert everything that's not str manually. Or you just print them out one after another, using commas. In my opinion, the most readable way will be using an f-string: print(f'This costs {18+24} dollars for {"Bob:Alice:Tom".split(":")}')
18th Aug 2019, 5:43 PM
HonFu
HonFu - avatar
+ 2
print("This costs " + str(18+24) +" Dollars for " + str("Bob:Alice:Tom".split(":"))) print("This costs", 18+24, "Dollars for", "Bob:Alice:Tom".split(":")) # This costs 42 Dollars for ['Bob', 'Alice', 'Tom']
18th Aug 2019, 5:16 PM
Diego
Diego - avatar
+ 1
a = [1, 2, 3] b = "4 5 6" You can do: print(a + list(b)) #Output: [1, 2, 3, '4', ' ', '5', ' ', '6'] print(str(a) + b) #Output: [1, 2, 3]4 5 6
18th Aug 2019, 6:03 PM
Seb TheS
Seb TheS - avatar