12
var1 = 'abc'
var2 = 'xyz'

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

... except for automatic spaces with ',' whats the difference between the two? Which to use normally, also which is the fastest?

Thanks

Scott Griffiths
  • 21,438
  • 8
  • 55
  • 85
eozzy
  • 66,048
  • 104
  • 272
  • 428

3 Answers3

26

(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.

  • which is the fastest among the two? – eozzy Jan 09 '10 at 12:34
  • They do different things, you cannot reasonably compare them for speed. That said, you can compare *printing* the two different ways, use the timeit module. –  Jan 09 '10 at 12:40
  • 1
    Note that for the timings above, cStringIO is almost certainly going to perform differently than writing to the console, and when writing to the console the performance can differ based on the platform you are on and the size of strings you are dealing with. – Dobes Vandermeer Aug 30 '13 at 16:03
6

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
Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
1

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.

user254875486
  • 11,190
  • 7
  • 36
  • 65