Python 3, an Illustrated Tour Transcripts
Chapter: Strings
Lecture: Walk-through: f-Strings

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're going to look at fstring test, open that up.
0:03 The first part says assuming the variable stock and price exist
0:06 make a variable res that has Stock and then whatever the stock value is
0:12 then price and whatever the price value is.
0:14 You should use an fstring to do this.
0:16 So long behold here is stock and price.
0:19 So let's make a variable called res, that's an fstring.
0:22 So to make an fstring we put an f in front of our string
0:25 and I'm going to say Stock here and then I want the value of stock,
0:31 in this case I'll just put it in curly braces here
0:34 and then price here with the value of price.
0:38 We could do some formatting here.
0:43 In this case, it didn't particularly define that we include any specific formatting.
0:47 Let's just run this and see if it works.
0:53 And it looks like it worked here.
0:55 So this will give us a string that looks like that, pretty cool.
0:59 The next part says assume the variable x exists.
1:02 And x is defined down here, create a variable answer
1:05 that has the sin of x using math.sin to 2 decimal places.
1:10 And so it wants us to say x with the value of x then sin of x
1:15 with the sin of that, use an f string to do that.
1:17 So the first thing we need to do is import the math libraries,
1:20 we're going to say import math here.
1:24 Let's come down here and we'll make this variable called answer.
1:27 It's going to be an fstring, so we'll put that in there.
1:31 We need x and we'll put the value of x there
1:35 and we want sin of x with math.sin of x and we need to format it.
1:46 So in order to provide formatting, we put a colon at the end here
1:50 and we're going to format this as a float.
1:53 So, I believe we need to say .2f to get 2 decimal places.
1:59 Let's try that and see if it works.
2:08 Oh, I've got a typo here, I need to close off my embedding of my expression there.
2:15 Let's try again.
2:17 Okay, it looks like that worked.
2:22 So this shows you that not only can you put expressions in here,
2:25 but you can put formatting with them as well using the formatting syntax.