- [Instructor] In this video, I'd like to take a look
at the String Types Format Method.
Now, the F-strings or format strings that we've been using
throughout these videos so far,
were added to the Python language back
in Python version 3.6.
As you can see here,
I'm currently using Python version 3.7.3.
And we do use the F-strings throughout our videos and books.
However, you will likely encounter older Python code
that uses the format methods.
So I did wanna make sure that I present that to you as well.
Now the way the format method works
is you create a format string,
and then you invoke the format method
on that string passing as arguments to the method,
the values that should be inserted.
So for example, let me go ahead
and paste in a sample snippet.
Here you can see we have a format string
and in this format string the specifiers says
that we would like to format a value
as a floating point number with two digits.
So the right of the decimal point,
we're invoking on that format string, the format method
and the value that we'd like to format
in this case is 17.489.
So as you can see, we do indeed get two digits to the right
of the decimal point in the resulting strength.
Now, you may be wondering what if there are multiple values
to be inserted?
Well, you can have multiple curly braced placeholders
in your format string, and you can supply multiple arguments
to the format method.
So here is a simple snippet, in which all we wanna do
is to insert the values Amanda and Cyan
into the two placeholders in the format string,
and they will be inserted in the order
that they're specified by default.
So as you can see, we get Amanda and then Cyan.
Now when you're dealing with internationalization
and localization, you might find it handy
to reference arguments in the format methods parentheses
by their position numbers,
and then use different format strings
depending on the language that you're working with.
So here's an example, in which we have three placeholders,
two of which have the same index number in them,
and one of which does not.
And as you can see, we only have two arguments,
but the argument at index zero, the first argument to format
will be placed twice into this format string.
And the argument in the second position,
index number one will be placed once into the format string.
So as you can see, we get Happy Happy Birthday
as a result of that.
You can also use keyword arguments as well,
which can make these format strings more readable.
So for example, here we have a format string
in which we've defined keywords first
and last as the names of those placeholders.
And then in the argument list,
we can say first equals Amanda
and that value will be placed here.
And then we can say last equals Gray
and that value will be placed here.
And perhaps more interesting is the fact
that of course, you can change the order.
So last and first, same arguments first and last.
But now they come out in reverse order
because by the name it's inserted into the correct
placeholder in the format string.