- In this self check exercise we'd like you to use
the string method format that we just introduced
in the preceding video to re-implement
the last three self check exercises.
So to make that easier for you I've gone ahead
and copied in those descriptions
from the previous three self checks.
So go ahead and take a look at the descriptions again here.
But this time rather than using F strings,
use the format method being called on a format string.
Go ahead and pause the video to try that then
come back and see the answers.
Okay, let's go ahead and reveal the answer for part A here.
So again we were using the type specifier C
to display the character codes 58, 45 and 41.
So I'll go ahead and expand that here.
Now as you can see we have three placeholders
all of which have a colon and a C.
The colon introduces the type specifiers and the type
specifier C says to take a numeric value and display
its character representation.
So 58, 45, and 41, when we display those again are going
to give us the colon, the dash, and the smiley face.
In this case, we had spaces in between them which is why
the spaces are being displayed there.
So if I go ahead and get rid of those spaces and re-execute
you can see that we get the smiley face in that case.
Now separately for part B, we wanted to display Amanda
right, center, and left aligned.
So, in this case, we have square brackets around
a placeholder because we wanted to be able to see
the formatted values with their spacing.
And so we have so a placeholder here that says whatever
is at index position zero should be formatted
right aligned in a field of 10.
Whatever is at position zero, again Amanda, should be center
aligned in a field of 10 and whatever is at position zero
should be left aligned in a field of 10.
Notice that every one of these format specifiers is using
the index zero so we're repeatedly going to insert
that value throughout the format string.
And then print the string and as you can see,
we get the same results as two self check exercises ago.
And then finally for part C here, we wanted to format
these two floating point values.
Again, in 10 digit fields preceded by their signs
with thousands separators and two digits
to the right of the decimal point.
So if we go ahead and expand that out here.
Let's take a look at our format specifiers
both of them are identical.
We're going to be using a sign in front of a ten character
field using thousand separators and two digits to the right
of the decimal point for the floating point number.
And we did that twice here.
And as you can see, we do indeed
get that same formatted result.