Stupid string formatting question

Delaney, Timothy tdelaney at avaya.com
Thu May 16 22:04:32 EDT 2002


> From: Michael S. Fischer [mailto:michael+usenet at dynamine.net]
> 
> OK, here's an example.
> 
>     >>> def x():
>     ...   return "a", "b"
>     ... 
>     >>> print "%s/%s/%s/%s" % ("hi", "there", x())
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>     TypeError: not enough arguments for format string

Well, try

print repr(("hi", "there", x()))

and see what it gives you. You get a 3-element tuple, the last element being
a tuple itself.

To do what you want, you need to do

print "%s/%s/%s/%s" % (("hi", "there",) + x())

which will give you a 4-element tuple.

Always give enough of your code to replicate the actual problem you are
having. If you don't get the same error message with the code you intend to
post as with your real code, your code will be insufficient to get a useful
answer.

Tim Delaney





More information about the Python-list mailing list