Python 3, an Illustrated Tour Transcripts
Chapter: Strings
Lecture: Walk-through: String Formatting

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video, we're going to look at format test, open that up in your editor. Let's look at the first problem.
0:06 It says create a string variable f1 to show a stock price. It should have three placeholders,
0:10 using the format method to print given data like a name that has a string, price that has a float and change that as a float.
0:18 It should be able to print a string that has name with the name inserted a couple spaces,
0:25 then the price with a $ sign and the price formatted as a floating point, with two decimal places and the change format is %.
0:33 Let's see if we can do this here, f1 is equal to name: we need a placeholder for that, string name,
0:41 so we'll just put a placeholder there, it looks like we've got some spaces here.
0:45 So we'll just copy those amount of spaces and stick them down here below. And then, we'll say price and I need to put a $ sign here.
1:02 And this is going to be formatted as a float. So I'm going to put: with two decimal places and f
1:09 and put change and we're going to put our change in here. It's going to have two decimal places and it's going to be formatted as a percentage.
1:20 Let's try this to see if it works, so just hit run here. Okay, and so we've got f2 does not work but this part worked.
1:29 So we're down to the second part, create f2, it should look like f1 but have 10 spaces for each left-aligned placeholder.
1:37 So each one of these curly braces is a placeholder just copy this and plop it down here. Let's change this to F2 and if we want 10 spaces in here,
1:49 so in order to use 10 spaces, we can put a colon here and say left-aligned 10.
1:56 We just put a less than 10 at the front and that should give us 10 spaces here. Let's give it a try and see if it works. So let's run this again.
2:08 And it appeared to work. So simply by putting that less than 10 at the front that says I want to make this APPL here take up 10 spaces
2:19 and whatever's in there I want to be left aligned. Okay, great, f3, it should have 3 placeholders and should be able to create the f1 or f2 strings
2:29 the placeholders except the formatting string. So this is sort of meta here. So this is saying I want you to create something
2:36 that could create this or could create this and the end user is going to pass in the contents of this.
2:42 So, let's see if we can do that here, f3 is equal to a string and we're going to say name colon and then we're going to have a placeholder here.
2:57 But we want to be able to have curly braces in there because we want the output of the format to have curly braces.
3:04 So we've got to put two curly braces, if we put two curly braces, then that's considered an escape. So this would show our curly braces.
3:14 Then we need to put another curly brace pair inside for where we need to put our formatting. And we'll do our spaces here and price
3:25 and we're going to have to again do our curly braces and change 1, 2, 3, 1, 2, 3. So this is how you include curly braces, you simply use two of them
3:41 and then Python is not going to use those as placeholders, but rather it's going to just put a curly brace there. Let's try it and see if it works.
3:50 Okay, it looks like it worked and you can see in the test here it's saying I want you to format on f3 with empty, with nothing,
3:59 with a float, with two places and a percent with two places. And then with that, I want to call format again,
4:07 so that should have the placeholders now with the formatting strings in there. And if you do that, we should get back
4:14 to the original thing that we had back in f1 up above. Hopefully, you feel like you understand formatting a little bit more. There's a lot more to it.
4:25 Again, you can refer to the slide deck or the website that was referred to in the slide deck for more examples of how to do formatting.


Talk Python's Mastodon Michael Kennedy's Mastodon