Streamlining Your Print
00:00 In this lesson, you’ll learn how to streamline your print.
00:05
Suppose you have a string, "Tuesday"
and two integers—one, eggs
, equaling 2
and the other, bacon
, equaling 3
.
00:14
And you want to display all of them in a string that says "Tuesday's
breakfast is 2 eggs and 3 pieces of bacon."
One of the ways that you could try to do that is through string concatenation. The process of taking these integers and this string and putting them together into a a final displayed string is called string interpolation, which is just a fancy way of saying that you want to insert some variables into specific locations within a string.
00:42 And one way you could do that is with string concatenation.
00:50 That code really isn’t the prettiest, and keeping track of what goes inside or outside the quotes could be tough, and remembering all those spaces. Fortunately, there’s a much better way to interpolate strings. They’re called formatted string literals, or more commonly known as f-strings. Let me just show you an example.
01:09
It starts with an f
and then you open your string, and then you use {}
(curly braces) for the variables that you want to place inside there.
01:18 You’re still inside the string, and that’ll interpolate into the final, same string. Much easier for you as a developer to read it, or even somebody else eventually reading your code.
01:30
You can even insert expressions between the {}
. The expressions are replaced with their result in the string. So let’s say you had a couple integers again, n
and m
.
01:42
n
is assigned to 3
, and m
is assigned to 4
. And you created an f-string, and in the final pair of {}
, you have an expression of n
multiplied by m
, n * m
, and that will evaluate it.
02:02
Things to remember about f-strings. The string literal will start with the letter f
before the opening quotation mark. Variable names that are surrounded by {}
will be replaced by their corresponding values, without requiring the built-in function str()
. Python expressions between the braces are replaced with their result.
02:25
f-strings are available only in Python versions 3.6 and higher. In the earlier versions of Python, you could use .format()
, which looked like this.
02:36
You would have the {}
in the same places, but instead of having the names of the different values inside, you would have at the end of your string the method .format()
, and then inside the parentheses, you would need to put in order the replacement fields that you wanted for the different {}
. You could get the same result, but as you can see, it’s a little more confusing to read.
03:04 And if you have more than, say, two or three of these, it may be hard to keep track of what’s going on over here. f-strings are a lot nicer-looking and easier to kind of keep in your head.
03:18
Let’s review what you’ve learned with a few exercises. Try creating a float
object named weight
with a value of 0.2
, and then create a string object named animal
with a value "newt"
.
03:31
Use these objects to print the following string using only string concatenation. Now try that whole thing again, using the older .format()
type and empty {}
placeholders.
03:45 Now try it one more time and display the same string using an f-string.
Dick de Goede on June 8, 2023
You could also use the c-style :)
"%s's breakfast is %i eggs and %i pieces of bacon." % (day, eggs, bacon)
s150042028 on Sept. 23, 2023
ffloat = 0.2
animal = "newt"
print(ffloat, "kg is the weight of the", animal)
print("{} kgg is the weight of the {}".format(ffloat,animal))
print(f"{ffloat} kggg is the weight of the {animal}")
Become a Member to join the conversation.
deanwaters1991 on June 6, 2023
I feel like I learned in this video something i was asked to do in the last video. In the last lesson I was trying to work out how to write a line that included variables and strings but couldn’t work it out. Now I know! 😊👍🏼