Skip to content Skip to sidebar Skip to footer

Combining Two String Variables

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: a = 'lemon' b = 'lime' Can someone tell me ho

Solution 1:

you need to take out the quotes:

soda = a + b

(You want to refer to the variablesa and b, not the strings "a" and "b")

Solution 2:

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.

Post a Comment for "Combining Two String Variables"