Re: [Tutor] printing format with list

2008-01-24 Thread Kent Johnson
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])

The argument after % must be a tuple (or a single item) so just convert 
the list to a tuple:
   print """ Testing %i, %i, %i """ % tuple(a)

or create it as a tuple to begin with if that is practical...

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing format with list

2008-01-24 Thread Tim Golden
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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor