String formatting strangeness

Peter Hansen peter at engcorp.com
Fri May 13 09:32:11 EDT 2005


dark.ryder at gmail.com wrote:
> I must be doing something wrong, but for the life of me, I can't figure
> out what.  Here's the code snippet which is giving me grief:
> 
> print type(number), type(name), type(seconds // 60), type(seconds % 60)
> print "\t\t\t<section number=\"%i\" title=\"%s\" length=\"%i:%i\"/>\n"
> % [number, name, seconds // 60, seconds % 60]
[snip]
> 
> Wait, what?  The first line clearly identifies that the the first,
> third, and fourth elements are all integers, yet the error says that
> *lack* of integers is the problem.  If I change all "%i"s to "%d", I
> get the same problem, and changing to "%s" (hey, it was worth a shot)
> gives "TypeError: not enough arguments for format string" instead.
> Huh?  I see four placeholders and a four-element tuple.

Nope, you see a four-element list.  Try changing it to a tuple... ;-)

-Peter



More information about the Python-list mailing list