Python Forum

Full Version: Use of .format()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some questions regarding this code from tutorial:

table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
for name, phone in table.items():
    print('{0:10} ==> {1:10d}'.format(name, phone))
In the print statement what 1 in {1:10d} means ( if I change it with 0 it doesn't work )? Also, what "d" stands for? Dictionary maybe? I tested if code can work without it and the asnwer is positive.
"{} {}".format('blue', 'cat')
If you print that the output will be:

Output:
blue cat
But if you print:

"{1} {0}".format('blue', 'cat')
the output will be:

Output:
cat blue
The default order is:

"{0} {1}".format('blue', 'cat')
The number in {:10} means that 10 positions will be used. Even, if the string is only 4 characters long. It will be right aligned and the rest are spaces.

So blue has index 0 and cat - 1. It's where to position the format arguments into the string placeholders. 'd' is for numbers. 
See this