[Tutor] Why has this syntax with multiple arguments ceased towork for the print command?

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Dec 20 19:32:29 EST 2003


> You simply forgot to put your arguments between parentheses
> (to form a tuple):
>
>  >>> print 'You are: %s? and It is the %dth of Dec.' % (esp,
int(cnt))
> You are: you? and It is the 20th of Dec.
>  >>>
> P.S. It's not a matter of the print statement but of the
%-Operator and
> operator precedence. I. e. your statement is interpreted as:
>
> print ('You are: %s? and It is the %dth of Dec.' % esp),
int(cnt)
>
> instead of
>
> print 'You are: %s? and It is the %dth of Dec.' % (esp,
int(cnt))

And just to add the point that the parens aren't needed (although
recoommended)
if you didn't use the print statement, that is:

s = 'You are: %s? and It is the %dth of Dec.' % esp, int(cnt)
print s

would also work. It's just the fact that the comma acts as a
separator
within the print statement that causes Python to get confused and
require the parens.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list