Someone at the 2015-03-06 dojo had a question about the meaning of {0[0]} and {0[1]} in format strings for specifying arguments on page 224 of Learning Python, 5th edition. So we played with it below.


In [1]:
somelist = list('SPAM')
somelist


Out[1]:
['S', 'P', 'A', 'M']

In [2]:
somelist[0]


Out[2]:
'S'

In [3]:
somelist[2]


Out[3]:
'A'

In [4]:
'first={0}, last={1}'.format(somelist[0], somelist[-1])


Out[4]:
'first=S, last=M'

In [5]:
'first={0[0]}, third={0[2]}, another={1}'.format(somelist, 'hello')


Out[5]:
'first=S, third=A, another=hello'

In [6]:
somedict = {'hello': 'world', 5: 3}
somedict


Out[6]:
{'hello': 'world', 5: 3}

In [7]:
'first={0[hello]}, third={0[5]}, another={1}'.format(somedict, 'hello')


Out[7]:
'first=world, third=3, another=hello'