Concatenation Operator + or ,

前端 未结 3 887
伪装坚强ぢ
伪装坚强ぢ 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:16

    (You're using Python 3.x, where print is a function—in 2.x, print is a statement. It's a good idea to mention the major Python version—2.x or 3.x—especially when asking for help, because currently most people reasonably assume 2.x unless it's stated.)

    The first, print('literal' + var1 + var2), evaluates an expression and passes a single argument to print. The second, print('literal', var1, var2), just passes three arguments to print. This is almost the same output purely by chance: that's how print works. The second is not doing any concatenation, and is simply outputting each value separated by a space (which is print's default behavior).

    To be explicit: the plus in the expression is doing concatenation, but the comma is not doing concatenation.


    Timing: I got the results below; however, I believe this is biased because the strings are so short (e.g. longer strings could reverse the result), and in any case, printing as presenting in the question won't take long (you'll get better performance worrying about many other things instead).

    Note: Use python -m timeit --help for instructions on how to use timeit.

    $ python -m timeit -s 'from cStringIO import StringIO; out = StringIO(); a = "abc"; b = "def"' 'print >>out, a, b'
    100000 loops, best of 3: 7.68 usec per loop
    $ python -m timeit -s 'from cStringIO import StringIO; out = StringIO(); a = "abc"; b = "def"' 'print >>out, a + " " + b'
    100000 loops, best of 3: 4.67 usec per loop
    $ python -m timeit -s 'from cStringIO import StringIO; out = StringIO(); a = "abc"; b = "def"' 'print >>out, " ".join([a, b])'
    100000 loops, best of 3: 5.37 usec per loop
    

    In particular, notice each code will give the exact same output (it's meaningless to compare if one method gives the wrong results). The StringIO is an easy way to not print to the screen in these tests, but it could be affecting the results too.

    0 讨论(0)
  • 2021-02-04 12:17

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题