Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of .format()
#1
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.
Reply
#2
"{} {}".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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
https://docs.python.org/3.6/library/stri...ing-syntax
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020