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

2011-06-08 Thread eizetov
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

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

2011-06-08 Thread Prasad, Ramit
From: tutor-bounces+ramit.prasad=jpmchase@python.org [mailto:tutor-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of eize...@gmail.com Sent: Wednesday, June 08, 2011 3:11 PM To: tutor@python.org Subject: [Tutor] String formatting question with 's'.format() I'm working through

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

2011-06-08 Thread Evgeny Izetov
I see now, that example helps. Basically I use one asterisk to extract a list or a tuple and double asterisks for a dictionary, but I have to provide keys in case of a dictionary, like here: template = '{motto}, {pork} and {food}' a = dict(motto='spam', pork='ham', food='eggs')

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

2011-06-08 Thread Alan Gauld
eize...@gmail.com wrote '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: The * tells Python to unpack parts and treat the contents as individual values. format is