Concatenation Operator + or ,

前端 未结 3 904
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 11:25
var1 = \'abc\'
var2 = \'xyz\'

print(\'literal\' + var1 + var2) # literalabcxyz
print(\'literal\', var1, var2) # literal abc xyz

... except for automat

3条回答
  •  失恋的感觉
    2021-02-04 12:19

    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
    

提交回复
热议问题