When formatting a string with values from a dictionary, you can use the dictionary keys instead of explicity defining all of the format parameters. Consider this dictionary that stores the name and age of a person.

Here is an example of formatting the string with values from the person. This is bad! If we added another key-value pair to the person dictionary, we would have to change the string and the format arguments

person={'first':'Tobin','age':20}print('{0} is {1} years old'.format(person['first'],person['age']))# Output: Tobin is 20 years oldperson={'first':'Tobin','last':'Brown','age':20}# Bad: we have to change the replacement fields within# our string, once we add new valuesprint('{0}{1} is {2} years old'.format(person['first'],person['last'],person['age']))# bad# Output: Tobin Brown is 20 years old

By using the dictionary keys in the string we are formatting, the code is much more readable and explicit.

person={'first':'Tobin','age':20}print('{first} is {age} years old'.format(**person))# Output: Tobin is 20 years oldperson={'first':'Tobin','last':'Brown','age':20}print('{first}{last} is {age} years old'.format(**person))# Output: Tobin Brown is 20 years old

Going even further, the same result can be achieved with your own objects by using obj.__dict__.

classPerson(object):def__init__(self,first,last,age):self.first=firstself.last=lastself.age=agedef__str__(self):return'{first}{last} is {age} years old'.format(**self.__dict__)person=Person('Tobin','Brown',20)print(person)# Output: Tobin Brown is 20 years old