Welcome back. There's one more method for strings that's a little more
complicated and important enough to warrant its own video segment.
We can concatenate a string together including
some hard-coded parts and some variable parts like this one.
We've got a hard-coded part, "Hello, period,
your score is," and then we've got some variable parts like a name and a score.
If I run it, where you concatenate, we get, "Hello",
we get "Rodney Dangerfield" that's coming from name,
your score is minus one, that's coming from there."
So, one thing to notice about this is it's just sort of a little hard to read.
It's a little hard to read this and figure
out exactly what it's going to look like over here.
You got to be careful about things like getting the period,
which really belongs with the end of the name,
but you got to stick it on there.
You got to remember to put spaces there.
You have to remember that score is a number,
so we got to pass it through the STR function in order to turn it into a string.
It's a lot of stuff that's not so convenient here and that's are readable.
So, we're going to have a new command,
the dot format method that's going to make this a little cleaner.
Before we get to that though,
let's just do a slightly more complicated version of this,
where instead of saying just Rodney Dangerfield score,
that we're going to get a whole list of tuples.
So, we've got Rodney Dangerfield with a score of minus one,
Marlon Brando getting one,
and of course you would get a 100.
We're going to loop through all of these people.
For each of them, we're going to print out something like that,
like what we did for Rodney Dangerfield.
So, " Hello Rodney Dangerfield.",
"Hello Marlon Brando", and "Hello you."
What I'm going to do next,
is show you something that's going to
replace that concatenation with something that's a little cleaner.
So, I've just replaced line five with something that is a little cleaner.
We have a first part of it which is called the format string,
and it's got some things in it which are places where we're going to do substitutions.
So, we have a complete format string and you can look at that
and get a sense of what the shape is of the thing that's going to come out at the end,
but we're going to have some substitutions.
This first set of curly braces says,
do a substitution here and we have positional replacements.
So, the first value is going to
get stuck in there and the second value is going to get stuck in here.
So, looking at this, we have a format string and then we are calling a method on it,
the dot format method.
So, it's a method on that string,
and we're passing in a couple of parameter values.
The first parameter value is going to get substituted in for the first curly braces,
the second parameter value for the second curly braces.
So, when I run this,
the first time we get to line five,
name will be Rodney Dangerfield and score will be minus one.
That's what's going to get substituted into the hello string,
and the next time it will be Marlon Brando and so on.
So, we get the same result as before but our code is a little more readable.
We'll be doing this more and more as the specialization goes on using
these format strings rather than concatenating together strings out of individual parts.
So, a little vocabulary that we have here.
We have the format string, we have parameters,
and these are the parameter values that are getting passed in,
and then we have references post to substitute in those parameters.
Here's another example that's a little more complicated where we can get some hints to
the format method telling it how many decimal places to use for numbers.
In this program, we're first asking the user to type
in an original price for an item and then a percentage that it's going to be discounted.
We compute a new price from the original price and the discount and then we're going to
just create a string and finally print it out.
The string that we're going to create is we're going to have
a whole sentence that includes some information about the original price,
how much the discount was,
and what the new price is,
and those things are going to get substituted in.
The original price will go here,
the discount will go here and the new price will go here.
The new thing that we're seeing here is this colon 0.2f.
What that says is the thing that's going to get
substituted in here is a floating point number,
that's what the f is saying,
and the period two is saying after the decimal point have two more digits.
If I run this, let's see what it looks like,
I'm going to have an original price of 89.99.
We're going to get a 20 percent discount and that comes out
saying that 89.99 discounted by 20 percent is 71.99.
Now, it isn't the case that 20 percent off of 89.99 is exactly 71.99.
It's actually a tiny bit less than that but
the closest number that's in dollars and cents is $71.99.
If I told the Python interpreter that I wanted four decimal points instead of two,
then it would print it out with more precision.
Let's take our 89.99 item,
get a 20 percent discount,
and now it turns out it's 71.9920.
So, we can control how many numbers we get after
the decimal place by specifying it inside the curly braces.
If we remove the hint entirely about how many decimal places to use,
then it just goes and uses its default.
In this case, it's giving us three decimal places.
If we had some different value it might choose to give
us more decimal places or less because we
weren't giving it any instructions on how many did do
is using its built-in algorithm to decide it.
If we want to control exactly how it comes out,
that's when we use this little hint.
For dollars and cents, it really makes sense to have
two decimal places because that's the precision for dollars and cents.
All right. So, that's the format statement.
Just a reminder, a format statement begins with a format string,
and then we call the dot format method on it,
and we pass in any values that are supposed to get substituted.
The substitutions happen where the curly braces are. I'll see you next time.