‫Welcome back.
‫Let's talk about the topic of formatted strings.
‫Up until now, we've just written simple strings, but we want a program that's dynamic, that's static.
‫Let's say we have a Amazon page and we're working at Amazon, actually.
‫And when a user logs into their profile, well, we want to display their name or what they have in
‫the cart.
‫In that case, we don't want to just hard code and write every single user's name like Andre or another
‫user Joe, and write every single name in the world.
‫No.
‫Ideally what we can do is have something dynamic where let's say it's a profile page and we simply use
‫the name variable and we display that on the page.
‫Now this name variable should equal whatever the user's name is, and this is something that we can
‫grab from the database again, something that we'll talk later on in the course.
‫But let's assume that in here when we do equals, we're going to grab some user information.
‫Which will be the name.
‫And this name for now will be John or Let's Go, Johnny.
‫And this Johnny we want to display on the profile page.
‫And we can do that with formatted strings.
‫We can simply do something like print and then say name.
‫But we also want to greet that person.
‫Right.
‫So let's do.
‫Hi.
‫And then plus name and remember to add a space.
‫If I click run, I get.
‫Hi, Johnny.
‫But as we get more and more information, let's say we have age and Johnny is 55.
‫If I wanted to extend the sentence and I want to say you are and then add another plus, make sure we
‫add a space in here and then add your old.
‫Ron here.
‫Hmm.
‫All right, we get a type error.
‫Must be string, not int.
‫Oh, boy.
‫All right, so we have to convert this into a string.
‫So remember, we can do that like this.
‫And then if I click Run.
‫Hi, Johnny.
‫You are 55 year old or years old.
‫All right.
‫That was a little cumbersome, but we're making our Amazon page dynamic.
‫We can grab different information from the database and we'll have in a string something according to
‫the user.
‫But there's a better way of doing this.
‫And with formatted strings, all we need to do is add an F at the beginning.
‫And this F at the beginning is going to tell Python, hey, this is going to be a formatted string.
‫And instead of doing all this plus and name and all this stuff and doing the STR to convert the type.
‫We can simply do something like this.
‫Let's remove this and simply do brackets and say name.
‫And then again remove all of this.
‫And say h.
‫So I'm going to say hi name.
‫You are h years old.
‫And if I click run here.
‫You see that it still works.
‫This is a new feature of Python three by adding F to the beginning.
‫It's saying, Hey, this is going to be a formatted string and I want you to just make these variables
‫available as strings inside of, well, this string.
‫How much cleaner is that?
‫Now, although this is nice and clean and this is my preferred way of writing strings before Python
‫three, you didn't really have this.
‫So in Python two and mind you, this works in Python three as well.
‫As you can see, we had something different to accomplish this.
‫What we had was this idea of a dot format.
‫And DOT format, so let's remove the F here is going to do the same thing for us.
‫We do a bracket here and then we say, Hey, we want Johnny.
‫And age of 55, and we can just remove these variable names.
‫If I click run here.
‫Hmm.
‫I get an error, and you have to be careful here.
‫Dot format works on strings, but you can see over here that we did it outside of the brackets.
‫And what it's doing is saying, hey, run the format action on this print.
‫But print is not really a string, right?
‫So you have to make sure that we move the brackets to the outside so that we evaluate this piece of
‫code first.
‫So the string is going to get formatted and then we're going to print.
‫If I run this.
‫Hi, Johnny.
‫You are 55 years old.
‫So that works the same way.
‫But what if we wanted to use variables?
‫Because right now we're just doing this by order.
‫So whatever comes first gets filled in first in the brackets.
‫Well, we can do something like name.
‫And H.
‫Let's see.
‫Can we do that?
‫Let's run.
‫There you go.
‫That works the same way.
‫What if we had a specific order?
‫Maybe we want this to be age and here to be name.
‫Well, we can mix those around.
‫We can say one here and zero here, because in computer science, we always start counting from zero.
‫So this is zero and this is one.
‫If we had something else in here, let's say a third variable, then this will be.
‫To.
‫So if I click run here.
‫All right.
‫Everything is upside down.
‫Finally, I can just create my own variables if I wanted to.
‫So let's say hi.
‫Let's say new name equals.
‫Sally and then age of Sally is going to equal, let's say, 100.
‫Sally is very old.
‫Now, if I do zero, let's say on one, here I click Run.
‫So now we'll actually get an error tuple index out of range.
‫Now, we haven't really learned about tuples and ranges, and this is a little confusing, but a bit
‫of a trick here.
‫We want to make sure that we add now because we've given.
‫The actual variable, a value we need to actually say new name here.
‫And here will be H.
‫So that if I click Run.
‫There you go.
‫Hi, Sally.
‫You are 100 years old.
‫As you can see with DOT format, things are a little bit more complicated.
‫You'll still see this and all python to.
‫Code still uses the DOT format and you'll see a lot of Python three code bases that still use format
‫because some people prefer it.
‫But I would argue that the F.
‫At the beginning of a formatted string is the way to go because.
‫Well, it just makes things so much easier, right?
‫Nice and clean, nice and easy.
‫So I do recommend that you use the formatted string with the F in front.
‫And for short, we usually call this an F string.
‫All right.
‫I'll see you in the next one.
‫Bye bye.