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

hcohen2 hcohen2 at comcast.net
Sat Dec 20 21:38:35 EST 2003


Alan Gauld wrote:

>>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
>
>
>
>  
>
I just would like to thank everyone for their quick and knowledgeable 
replies.  I look forward to the time when I can reciprocate by answering 
questions posed on this forum.




More information about the Tutor mailing list