I often use Python's print
statement to display data. Yes, I know about the '%s %d' % ('abc', 123)
method, and the '{} {}'.format('abc', 123)
method, and the ' '.join(('abc', str(123)))
method. I also know that the splat operator (*
) can be used to expand an iterable into function arguments. However, I can't seem to do that with the print
statement. Using a list:
>>> 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 this simply not possible? What exactly are the things that follow print
? The documentation says that a comma-separated list of expressions follow the print
keyword, but I am guessing this is not the same as a list data type. I did a lot of digging in SO and on the web and did not find a clear explanation for this.
I am using Python 2.7.6.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…