Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Could anyone explain String formatting with dictionaries challenge step-by-step?

I've tried a variety of things ranging from following the example in the video to going through many questions about the same challenge asked by other students and answers from other students or mods.

Not sure what I'm missing or just not understanding. I'd really appreciate some help on the matter. After 2 hours of attempts, I think it's time to ask for help.

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

#Code used in the video previous to this challenge
def unpacker(first_name=None, last_name=None):
    if first_name and last_name:
        print("Hi {} {}!".format(first_name, last_name))
    else:
        print("Hi, no name!")
unpacker(**{"last_name": "Wick", "first_name": "John"})

1 Answer

csr13
csr13
33,290 Points

Whenever you use " ".format() you can unpack a dict by using **, so you need to unpack (not manually) the dict to the format() function.

If you were to unpack the dict manually you would do this:

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

But you can also use ** to your benefit to allocate the keys in their reserved placeholders without unpacking a dict manually.

By using ** I think what happens is you are unpacking the dict and passing it as keyword arguments that fit the reserved placeholder, but at the same time creating those named parameters.

This:

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

Is the same as this:

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

If the dict passed to the funcion is like this.

dict_passed_to_function = {'name': 'yourname', 'food': 'yourfood' }

Hope this helps :dizzy:

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Thank you!!! Really, thank you. I really appreciate that you took time out of your day to help me out. This really cleared things up. I can't believe it was this simple. For some reason, I got it all twisted in my head.