Skip to content Skip to sidebar Skip to footer

Concatenation Operator + Or ,

var1 = 'abc' var2 = 'xyz' print('literal' + var1 + var2) # literalabcxyz print('literal', var1, var2) # literal abc xyz ... except for automatic spaces with ',' whats the differe

Solution 1:

Passing strings as arguments to print joins them with the 'sep' keyword. Default is ' ' (space).

Separator keyword is Python 3.x only. Before that the separator is always a space, except in 2.5(?) and up where you can from __future__ import print_function or something like that.

>>>print('one', 'two') # default ' '
one two
>>>print('one', 'two', sep=' and a ')
one and a two
>>>' '.join(['one', 'two'])
one two
>>>print('one' + 'two')
onetwo

Solution 2:

Using a comma gives the print function multiple arguments (which in this case are printed all, seperated by a space. Using the plus will create one argument for print, which is printed in its entirety. I think using the + is best in this case.

Post a Comment for "Concatenation Operator + Or ,"