Using the * operator (splat) with print

I often use the Python operator printto display data. Yes, I know about method '%s %d' % ('abc', 123)and method '{} {}'.format('abc', 123)and method ' '.join(('abc', str(123))). I also know that the splat ( *) operator can be used to extend the iteration into function arguments. However, I cannot do this using the instructions print. List Usage:

>>> l = [1, 2, 3]
>>> l
[1, 2, 3]
>>> print l
[1, 2, 3]
>>> '{} {} {}'.format(*l)
'1 2 3'
>>> print *l
  File "<stdin>", line 1
    print *l
          ^
SyntaxError: invalid syntax

Using a tuple:

>>> t = (4, 5, 6)
>>> t
(4, 5, 6)
>>> print t
(4, 5, 6)
>>> '%d %d %d' % t
'4 5 6'
>>> '{} {} {}'.format(*t)
'4 5 6'
>>> print *t
  File "<stdin>", line 1
    print *t
          ^
SyntaxError: invalid syntax

Am I missing something? Is it just not possible? What exactly happens after print? The documentation says that the list of expressions separated by commas matches the keyword print, but I assume that this is not the same as the list data type. I did a lot in SO and on the Internet and did not find a clear explanation for this.

I am using Python 2.7.6.

+4
1

print - Python 2.x *. print, :

print_stmt ::=  "print" ([expression ("," expression)* [","]]
                | ">>" expression [("," expression)+ [","]])

, print *.


* , , print Python 3.x. , __future__:

from __future__ import print_function

:

print(*l)

:

>>> # Python 2.x interpreter
>>> from __future__ import print_function
>>> l = [1, 2, 3]
>>> print(*l)
1 2 3
>>>
+10

All Articles