I am reading the Python documentation on fancier output formatting and they have example code which is confusing. In the following code:
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
What does the 0 refer to in {0[Jack]:d} and {0[Sjoerd]:d}. The tutorial's explanation omits any hint about the function of those zeros.
This video tutorial about formatting strings uses the same syntax without explaining it clearly too!
If I put a 1 in the place of 0 I get the error:
IndexError: tuple index out of range
What on Earth has the tuple have to do with it?
Taken from the Documentation:
If you have a really long format string that you don’t want to split up, it would be nice if you could reference the variables to be formatted by name instead of by position. This can be done by simply passing the dict and using square brackets '[]' to access the keys
0refers to the first argument offormat, i.e.table.