 In this video, we're going to talk about the print function. I'm going to write a program in the upper half of the Thani window, and whenever I write a program, I always start off with a short description of the program, my name, and the date that I wrote it, and then a longer description of what the program does and the purpose of the program, namely given an age in years, calculate the approximate age in days. I want a variable to hold the number of years, and so I'll call that years, that's a fairly descriptive name, and I'll set that to my current age, which is 64, and then I'm going to calculate the total number of days, and that's going to be 365 times the number of years, and then I'm going to print days. This is the simplest form of the print function, you just give it a variable. Let's save that program, and I'm using Linux, by the way, so if you're using Windows or macOS, your saved dialog will not look anything like this. And let's go to the Commas C020 file, and in there we'll call this age1.py. And now let's run the program, and when I run it I get the number 23,360. This works, but it's not ideal. If I gave this program to someone else and they ran it, all they would see is a number with no explanation. So whenever you print something, you really want to label it, so the people who are using your program will know what it is you've tried to print. Print can print as many things as you want. So I can say your age in days is approximately, and then the word days. Excuse me, the variable days. That's an important point here, by the way. Days is not in quote marks, so that means I'm actually accessing the variable. I'm not printing the word, D-A-Y-S. I'm printing whatever is in the variable days. Big difference. This print function call is going to print the words, your age in days is approximately, and then it'll print whatever is in the variable days. Let's save that and run it, and this time it says your age in days is approximately 23,360. So far, so good. Remember I told you I could print anything I wanted, as many things as I wanted, so let's put a period at the end of the sentence. Save and run. Well, let me clear the shell first, because I don't want to interfere with the subtitles of the closed captioning at the bottom of the screen here. And let's run this again. And now it says your age in days is approximately 23,360, and there's a space before the period. That's because print always prints with spaces in between the items that you've separated by commas. Gee, wouldn't it be nice if there were a way to turn that off? Well, there is. One way to do it is, at the very last things, I'm going to say sep equals and then an empty string. Sep stands for the separator. Now it's going to print the items that I've listed inside of the print without any separation between them. Let's save it and run. And this time it says your age in days is approximately 23,360, period. Because there are no separators, this means that if I want spaces in between words and numbers, I have to put them in myself. Put a space inside the first set of quote marks so that I will get the spacing between the word approximately and days. Save it and run it. And that's exactly the result I desired. Okay, there must be a better way, because this is a little bit clunky to have to remember when to put in spaces and when not to and how to put in the sep equals and when not to. Turns out there is a better way as a little bit weird looking, but here we go. I'm going to say your age in days is approximately and then an empty pair of curly braces. This is a placeholder and then a period. That string, I'm going to say, I want you to format that and fill in the blank, those curly braces here. You can think of them as fill in the blank with days. If I run that, I get exactly what I wanted. But there are two ways you can do it. You can say, print a bunch of separate items and then use sep equals to make sure the spacing comes out right. Or you can make a single string and put in curly braces as placeholders and then pass that to the format function and tell the format function what things ought to fill in those curly braces. You can have as many sets of curly braces as you want, by the way. For example, I could say your name equals and this case I'll put David and then I could put here curly braces comma your age in days is approximately curly braces. Now I have two sets of fill in the blanks, which means that format has to have two things to fill them in. And so I'm going to format this string filling in the first set of curly braces with your name and the second pair of curly braces with days. Save that and run it. Well, clear the shell first, excuse me. Got to remember those closed captions. And I'll say David, your age in days is approximately 23,360. Format is really powerful. You can do a lot of wonderful things with it. Here's another program. In this program, I want to find out what a percentage is. What's 7.5% of something that costs, let's say, $19.95. My new program here is going to be calculate percentage of a monetary amount. And again, I'm going to put my name and the date. The purpose of this program is, given a price and a discount percentage, what is the amount of money to be saved? That's what I'm trying to calculate. Let's say my amount is going to be $19.95, and my percentage is going to be 7.5. So I'm going to have 7.5% of $19.95. Well, the amount that I saved is going to be the percentage divided by 100 times the amount. I can print that out as some percent of some monetary amount is another monetary amount. And I'll put a period at the end of my sentence, and I'll pass that on to the format function. There's three sets of curly braces. That means I have to fill in them with three variable names. So the percentage is the first one. The second set of curly braces is going to be my original amount, and the third will be the amount of money saved. When I press Enter, you'll notice that it's indented for me. That's a signal that something is not right. Why didn't it go to the new line? The reason it didn't go to the new line is because I forgot to close my parentheses on line 9. I need that second set of parentheses, and now that gray highlighting goes away. I'm good to go. Let's save that, and let's call this discount.py. And clear the shell. Run it. Seven and a half percent of 1995 is 1.49624999999. Oh my goodness, that will not do. We need to make format give us exactly two decimal places after the decimal point. The way you do that is inside the curly braces, you put what's called a format string, a colon. And in this case, I'm going to say I want decimal point two, two decimal places, and this is a floating number. I want that for the amount saved as well. And for the percentage, let's do the percentage of three decimal places. So I'm going to say colon dot 3f. Save and run. And now I get the percentages and the monetary amounts with exactly the number of decimal places that I desire. There's a web page I've written that describes some of the other things that you can do with these format strings. I strongly suggest you read it. And that's a whirlwind tour of the print function.