[Tutor] String formatting question with 's'.format()

eizetov at gmail.com eizetov at gmail.com
Wed Jun 8 22:10:39 CEST 2011


I'm working through the 'Learn Python' book by Mark Lutz, in this example:

>>> somelist = list('SPAM')
>>> parts = somelist[0], somelist[-1], somelist[1:3]
>>> 'first={0}, last={1}, middle={2}'.format(*parts)
"first=S, last=M, middle=['P', 'A']"

why do we need the '*' at 'parts'. I know we need it, because otherwise it  
gives an error:

Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
'first={0}, last={1}, middle={2}'.format(parts)
IndexError: tuple index out of range

Still, wouldn't python basically see 'parts' and insert the actual tuple  
instead of the variable 'parts'? How does the machine think?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110608/049bd9b7/attachment.html>


More information about the Tutor mailing list