Instructor: Hi guys and welcome back.
In this video, we're going to talk about
string formatting in Python.
Specifically, we're going to be using
a newer Python feature from Python 3.6 onwards.
So for this video,
make sure that you're using Python 3.6 or later.
Let's say that you've got two strings,
one, which is Bob, and has the name of 'name,'
and one which is Hello, Bob and has the name of 'greeting.'
If you print greeting out, of course
you're going to get,
Hello Bob, since that are the contents of the string.
But if you set 'name' to Rolf
and then you print greeting again,
then naturally as you run that,
you're gonna get Hello, Bob and then Hello, Bob again.
Greeting contains the string name, which is Bob,
but it doesn't calculate it dynamically,
which means that when greeting is defined,
it contains the characters Hello comma space Bob.
And then changing 'name' doesn't change that greeting.
So if you wanted to change the greeting,
you would have to do something a little bit different.
Indeed, instead of typing, Hello comma Bob,
what you'd like to do is to put an 'F'
in front of the quotation marks.
So now we've got what's called an F string in Python.
An F string allows us to embed variables
inside of strings.
So here instead of Hello comma Bob,
we can do Hello comma 'name' inside these curly braces.
Now, again, we've got F and then inside the string,
we can use the curly braces and inside the curly braces,
we can put a variable name.
So when this is calculated,
'name' will be replaced by Bob,
so we will print out Hello, Bob.
So if we run that,
you'll see down here that we get Hello, Bob.
So exactly the same thing as before.
At this point, I'll say that if you do
what we had earlier and set 'name' to Rolf,
and then you print greeting again,
you'll have the same problem as before.
So if you press play again,
you'll see Hello, Bob, Hello, Bob.
So what we've done here is we have embedded
the variable inside the string
with the value that that variable had
at this point in time, in line 2.
So in line 2, 'name' had the value of Bob,
so greeting is Hello, Bob.
So what is the point of using the F string
if it doesn't change dynamically?
Well, the good thing about this F string
is that instead of defining a variable for it,
if you wanted to have a different value each time,
you could just print it out, like that,
and get rid of this variable definition.
Now what we've got here is we are printing
and inside the brackets, we've got
the value that we want to print,
so here we have the F string,
which includes an F, then the quotation marks,
and then the characters inside it, including the variable.
By doing something like this,
now every time you run this code,
you're going to use the name at its latest point in time,
so we get Hello, Bob, and Hello, Rolf.
There is a way of defining a template,
which instead of calculating it at
the time where it is defined,
you can calculate the values
to be replaced inside it later on.
Here's how you do that.
So you can define your greeting as 'Hello,'
and then open and closing curly braces.
Notice that we don't have an F in front of it.
Then you can say that with 'name,'
which is another variable,
is greeting.format and then 'name.'
So what we've done here
is we have created now three strings.
We have the string 'name,' which has the value Bob,
we have the string 'greeting,' which is our template,
which says, "Hello comma curly braces
and finally we say, greeting.format
and then inside brackets, we put in the name.
So what this is doing is essentially it's calling
the format function
inside of 'greeting' and what that's doing is,
it is giving the name to this template,
and what the template will do is it will
replace the curly braces
by the contents of the 'name' variable.
So 'with_name' will contain "Hello comma Bob."
If we print that out, you'll see what I mean.
You get Hello, Bob printed out.
One of the benefits of this is that you can then
use this 'with_name' with other things,
so you can reuse the template, the greeting,
and instead pass in Rolf, for example,
and then if you run this, you'll get Hello, Rolf.
So it allows you to define a template
that you can reuse with multiple values.
Notice that in here you can have one with Bob there,
then you can have one with Rolf
and you can do that everywhere then.
So now you have reused the template two times
to create two different strings.
This is why the format can be useful
instead of F strings, but generally,
you'll be using F strings
if you're using Python 3.6 or later.
Another benefit of using format
is that you can create longer templates.
For example, here we've got a longer phrase
that says Hello comma placeholder.
Today is placeholder.
So we can say longer_phrase.format
and then we can pass in Rolf and Monday.
What this will do is it will put the values
Rolf and Monday into the placeholders
one at a time, so Rolf will go into the first placeholder,
Monday will go into the second placeholder,
and then you can print this out,
and you will get back the appropriate value,
Hello, Rolf. Today is Monday.
Again, you could do this with many different strings,
so that is something that is available
if you'd like to use it,
but generally you'll be using F strings.
Those are the two main ways of
doing string formatting in Python,
which means, essentially, to embed values inside a string.
There are other ways which are older,
but we're not gonna be learning about those in these videos,
just because they're not really used all that much anymore.
Thanks for joining me in this video
and I'll see you in the next one.