Instructor: 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 on Amazon, actually.
And when a user logs into their profile,
well, we wanna display their name
or what they have in the cart.
In that case, we don't wanna just hard code and write
every single user's name, like Andrei 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 gonna 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 wanna 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 wanna greet that person, right?
So let's do hi, and then + 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 wanna say "you are" and then add another +,
make sure we add a space in here and then add year old.
Run 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 + 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 age.
So I'm going to say hi, name, you are age years old,
and if I click run here you see that it still works.
This is a new feature of Python 3.
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 3, you didn't really have this.
So in Python 2,
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 .format.
And .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,
.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 age.
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 1 here and 0 here,
because in computer science
we always start counting from 0.
So this is 0 and this is 1.
If we had something else in here
let's say a third variable, then this will be 2.
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 = sally
and then age of sally is going to equal, let's say 100.
Sally is very old.
Now, if I do 0, let's say and 1 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 age.
So that if I click run.
There you go, hi sally you are 100 years old.
As you can see with .format
things are a little bit more complicated.
You'll still see this in all Python 2 code
still uses the .format.
And you'll see a lot of Python 3 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.