+ 1
How to concatenate str & list
Eg " this costs" + str(11+56) + " Dollars " + "for" +"Bob:Alice:Tom".split(":")
3 Respuestas
+ 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(":")}')
+ 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']
+ 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