Can't wrap my head around Exercise 6 and 7

Im trying to comment my code in exercise 6 & 7, but are stuck two places where I’m not sure exactly what the code does.

Exercise 6
hilarious = False
joke_evaluation = “isn’t take joke so funny?! { }”
print(joke_evaluation.format(hilarious))

I’m not entirely sure what the .format does instead of using f""?

Exercise 7
end1 = “C”
end2 = “h”
end3 = “e”
end4 = “e”
end5 = “s”
end6 = “e”
end7 = “B”
end8 = “u”
end9 = “r”
end10 = “g”
end11 = “e”
end12 = “r”

#watch that comma at the end. try removing it see what happens
print(end1 + end2 + end3 + end4 + end5 + end6, end=’ ')
print(end7 + end8 + end9 + end10 + end11 + end12)

I litterally don’t get how the code figures out it need to put the last print statement inside the first. It looks like it somehow have figured out to make the last print() into a variable?

Hope someone can explain these two tings, as I’m sort of been going in circles by myself.

Hello @ktrager

I hope I can help you with your first question a little.

The {} is like a container waiting to be filled with a value from .format(a_variable).
This can be used both with Python 2 & 3.

The other thing I guess you mean ”f-string”.
It can only be used from Python 3.6 and above.
With the f-string you fill the {} with the variable to be printed.

grand_piano = ”Steinway”
print(f”If you have some money left you can choose the {grand_piano}”)

Output:

If you have some money left you can choose the Steinway

1 Like

Hi @ktrager, welcome.

Yes, the f before the “string” is called a format-string or f-string. When you see it, you can expect the {} container in the the string somewhere, (or even more than one) as you probably know.

.format is the old school way of adding variables or other items into a string. It’s also common in other languages. The example is this exercise is a little tricky but I expect that’s deliberate to get you thinking.

The syntax is like this:

example = “some text”
print(“I’m going to inject {} in a string!”.format(example))

The f-string replaces this by making it more readable which is a goal of Python:

example = “some text”
print(f”I’m going to inject {example} in a string!”)

Now look at Zed example and see if you can figure it out.

I’m not sure I understand you second question. Can you elaborate?

Thank you @ulfen69

so basically .format is the same as an f-string.
Except .format can be used for both Python 2 & 3 and an f-string is an easier way of doing the same?

Is that how it works?

Hi @gpkesley thank you for your answer…

Regards to question 2, I literally don’t understand it. Probably also why you don’t understand my question. Maybe it’s better to explain what I understand.

#1) creates 12 variables
end1 = “C”
end2 = “h”
end3 = “e”
end4 = “e”
end5 = “s”
end6 = “e”
end7 = “B”
end8 = “u”
end9 = “r”
end10 = “g”
end11 = “e”
end12 = “r”

#2) this is where I’m lost.
Im not sure what end=’ ’ refers to?
I know it makes the second print statement print on the same line as first statement with a space inbetween created by the ’ '.
But where does ‘end’ come from?

print(end1 + end2 + end3 + end4 + end5 + end6, end=’ ')
print(end7 + end8 + end9 + end10 + end11 + end12)

Hi @ktrager

In a way one can say they are almost the same. They both have {}. And one does not have to declare type int (%d) or string (%s) as when one use another older way of printing.
I guess f-string is a development from .format().

So I like f-string. It is very easy.

I figured this out - basically I was confused about end as in not to stop the first print line and its similarity with the 12 variable names.
Found it in another exercise

2 Likes

I think everyone set you straight on .format, but the 2nd thing is because you told print to use ’ ’ (space) as the end of the line when you wrote , end= ' '). Normally it prints a \n (newline) but you told it to print a space.

Thank you @zedshaw
What got me confused was end= " ") reminded me of the 12 variable names and thought ‘end =’ was some sort of shortening to group them together.

1 Like