combining two string variables [duplicate]

Each Answer to this Q is separated by one/two green lines.

I’m a novice Python user trying to do something that I think should be simple but can’t figure it out. I’ve got 2 variables defined:

b = 'lime'

Can someone tell me how to combine these in a new variable?

If I try:

>>> soda = "a" + "b"
>>> soda

I want soda to be 'lemonlime'. How is this done?


you need to take out the quotes:

soda = a + b

(You want to refer to the variables a and b, not the strings “a” and “b”)

IMO, froadie’s simple concatenation is fine for a simple case like you presented. If you want to put together several strings, the string join method seems to be preferred:

the_text="".join(['the ', 'quick ', 'brown ', 'fox ', 'jumped ', 'over ', 'the ', 'lazy ', 'dog.'])

Edit: Note that join wants an iterable (e.g. a list) as its single argument.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .