Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Vladimir Lapcevic
Vladimir Lapcevic
6,363 Points

Please suggest what is wrong with my code. Thanks

Please give me some explanations why this is not working. Thanks

string_factory.py
def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format()


favorite_food(**{"name": "Vl", "food": "ta"})

2 Answers

jassim Al-Hatem
jassim Al-Hatem
1,850 Points

unpack dict.

def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
jassim Al-Hatem
jassim Al-Hatem
1,850 Points

And I just wanted to add that if you actually want to unpack a dict. use this method.

def favorite_food(**kwargs):
    print('name: {} \n last name: {}'.format(kwargs['name'], kwargs['last_name']))

Student_details = {'name': 'Jassim', 'last_name':'Alhatem'}
favorite_food(**Student_details)
status 401
status 401
13,816 Points

Hi u need to remove the last line and inside the format command u need to unpack the dict. u do it by adding **dict