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

Gregor Lingl glingl at aon.at
Sat Dec 20 18:16:13 EST 2003



hcohen2 schrieb:

> I am certain code like this ran for me without any problem:
>
> print 'You are: %s? and It is the %dth of Dec.' % esp, int(cnt)
>
> both esp are gathered by ... = raw_input('Enter ...') and each works 
> properly as a single argument in test strings.
>
> Now I get:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: not enough arguments for format string

You simply forgot to put your arguments between parentheses
(to form a tuple):

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

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in -toplevel-
    print 'You are: %s? and It is the %dth of Dec.' % esp, int(cnt)
TypeError: not enough arguments for format string
 >>> print 'You are: %s? and It is the %dth of Dec.' % (esp, int(cnt))
You are: you? and It is the 20th of Dec.
 >>>

HTH, Gregor

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))



>




More information about the Tutor mailing list