Streamline Your Prints (Solution)
00:00
Here I am again in the interactive shell. And the task was to start by defining two variables. One was weight = 0.2
, floating point, and then animal
was a string that should point to the string "newt"
.
00:17 Okay, now I’ve got to work with this.
00:20 Assemble a string with these variables using concatenation,
00:31
str.format()
, and f-string.
00:37 Okay, let’s start with the concatenation.
00:40 I’m going to print it even though here in the console, we would get a result also if it didn’t print it. But let’s stick with this. And here again, I will have to convert the first one to a string because this is currently a float.
00:54
If I try to add a float and a string together using the +
operator, that would give me an error like you saw earlier. So I will say str(weight)
, convert it to a string explicitly, and then +
and here comes in handy that I copied the middle part of the string before, so I don’t have to type it out all the time.
01:17
And then I will concatenate that again with animal
. So it says 0.2 kg is the weight of the newt
, and then I guess there should be a period at the end to make it a proper sentence.
01:32
If I press Enter here, you can see that it says 0.2 kg is the weight of the newt.
So that’s a solution that works using just string concatenation and nothing else.
01:41
Now we’re going to try to use the .format()
method on the string.
01:45
So again, I open up the print()
function, and now I can use the curly braces as a placeholder. So this is a place where I want to insert something in the first spot.
01:57
I want to insert the weight. Then again comes the middle part of the string, "kg is the weight of the"
, and then I will use another pair of curly braces where I will insert the animal.
02:10
So this should say "newt"
, and then I can put a period there. And now this is my string template, kind of, where I want to insert these two variables that I’ve defined earlier.
02:23
And I can do that using .format()
. So on that string, "{} kg is the weight
of the {}."
I’m calling .format()
.
02:33
And now I will pass in the two variables that I want to insert into the two placeholders here. Into the first one, I want to insert weight
,
02:41
comma, and then animal
. And it’s going to take the first argument and insert it into the first placeholder and then the second argument and insert it into the second placeholder.
02:51
So if I now close the parentheses for the print()
call and then press Enter, then you can see I get the same output as before: 0.2 kg is the weight of the newt.
So that’s how you can do this using .format()
.
03:04 You can see that it makes it more readable already than just using the pure string concatenation. And it saves you the effort of having to do explicit type conversion on objects that aren’t strings.
03:16
And now, in my opinion, even a little more readable, is going to be the f-string. So I’m again opening up a call to print()
, and in here I will start by typing f
and then opening up the string by just doing two quotation marks.
03:30
And I will again use a curly braces placeholder. But now instead of keeping it empty, I will put in the variable name directly. So I’m going to say {weight}
,
03:42
then the middle part of the string, kg is the weight
of the
, open up another set of curly braces and input animal
directly into the curly braces and then a period at the end.
03:53 And that’s all you need to do for the f-string. If I press Enter now, you can see that again you get the same output as in the two other variants.
04:03
So these are three different ways of assembling this final string that gives the information and that contains both a string, the "newt"
string, as well as a string representation of a floating-point number.
04:15 All right, this is the way you can do this. Let me know which is your favorite.
Become a Member to join the conversation.