I don't understand it this


In this image what do format and {} actually do?
And why did we write it that way?

Hello @CodeNinja

The “{}” is a container for a variable.
In this case hilarious which is set to

False

In here there is another variable called joke_evaluation which is set to the string:

"Isn´t that joke so funny?! {}" 

When printed it all comes together. The string is printed + the container “filled” with hilarious which is equal to False.

“Isn´t that joke so funny?! False”

1 Like

Thanks a lot! I just saw it in the Ex 8 too. Why do we write it like this though? Why not just type it in?

Just to wrap your brain out side in :slight_smile:

There are even more ways to do this:

hilarious = False

print(f"True is not {hilarious}")
# This "f-string" requires python3.6 or above
print("True is not %s" % (hilarious))
print("True is not {}".format(hilarious)) # The way used in the book

They all gives the output:

True is not False

3 Likes

In terms of why do it like that…good question. This is a Zed tip bit. He drops these quirks in that make you go from thinking “I’m smashing this”… to “WTF?!”

There is always good reason and its a good learning technique. It’s good practice to make a note of these things as you encounter them so you can research later. You may feel that this will interrupt your progress, and it’s tempting to jump in and ask questions, but try to preserve.

(you will end up with a long list of things to research later, irrelevant of your coding ability, so best to start the practice now).

As for why might you want a placeholder rather than type it in…

Imagine you are receiving data from someone else evaluating the joke, like a :slight_smile: or :frowning: feedback app. You don’t want to duplicate that line, so you add a placeholder for that action, and manage the input data elsewhere.

Well, in this case yes you can just type it in.

Later on though, you’ll need to use this technique to do much more complex text insertions. Rather than throw those complex text insertions at you, I give you a simple one.