[Tutor] printing format with list

Tim Golden mail at timgolden.me.uk
Thu Jan 24 15:42:45 CET 2008


Andy Cheesman wrote:
> Hi people
> 
> Is there a way to use a list with printf formating without having to 
> explicitly expanding the list after the %
> 
> e.g
> 
> a = [1, 2, 3]
> 
> print """ Testing
>                 %i, %i, %i """ %(a[0], a[1], a[2])
> 

It looks as though string formatting only understands tuples,
not sequences in general:

a = [1, 2, 3]
print "Testing: %i, %i, %i" % tuple (a)

or just:

a = 1, 2, 3
print "Testing: %i, %i, %i" % a

TJG


More information about the Tutor mailing list