1 00:00:08,000 --> 00:00:12,900 Welcome back. There's one more method for strings that's a little more 2 00:00:12,900 --> 00:00:17,580 complicated and important enough to warrant its own video segment. 3 00:00:17,580 --> 00:00:21,000 We can concatenate a string together including 4 00:00:21,000 --> 00:00:26,805 some hard-coded parts and some variable parts like this one. 5 00:00:26,805 --> 00:00:33,509 We've got a hard-coded part, "Hello, period, 6 00:00:33,509 --> 00:00:41,680 your score is," and then we've got some variable parts like a name and a score. 7 00:00:43,940 --> 00:00:48,600 If I run it, where you concatenate, we get, "Hello", 8 00:00:48,600 --> 00:00:54,850 we get "Rodney Dangerfield" that's coming from name, 9 00:00:54,850 --> 00:00:59,960 your score is minus one, that's coming from there." 10 00:01:00,350 --> 00:01:07,250 So, one thing to notice about this is it's just sort of a little hard to read. 11 00:01:08,130 --> 00:01:11,380 It's a little hard to read this and figure 12 00:01:11,380 --> 00:01:15,240 out exactly what it's going to look like over here. 13 00:01:15,240 --> 00:01:18,820 You got to be careful about things like getting the period, 14 00:01:18,820 --> 00:01:21,880 which really belongs with the end of the name, 15 00:01:21,880 --> 00:01:23,730 but you got to stick it on there. 16 00:01:23,730 --> 00:01:26,540 You got to remember to put spaces there. 17 00:01:26,540 --> 00:01:29,570 You have to remember that score is a number, 18 00:01:29,570 --> 00:01:34,510 so we got to pass it through the STR function in order to turn it into a string. 19 00:01:34,510 --> 00:01:38,330 It's a lot of stuff that's not so convenient here and that's are readable. 20 00:01:38,330 --> 00:01:41,285 So, we're going to have a new command, 21 00:01:41,285 --> 00:01:45,395 the dot format method that's going to make this a little cleaner. 22 00:01:45,395 --> 00:01:47,525 Before we get to that though, 23 00:01:47,525 --> 00:01:51,275 let's just do a slightly more complicated version of this, 24 00:01:51,275 --> 00:01:55,220 where instead of saying just Rodney Dangerfield score, 25 00:01:55,220 --> 00:01:59,615 that we're going to get a whole list of tuples. 26 00:01:59,615 --> 00:02:03,550 So, we've got Rodney Dangerfield with a score of minus one, 27 00:02:03,550 --> 00:02:05,370 Marlon Brando getting one, 28 00:02:05,370 --> 00:02:07,830 and of course you would get a 100. 29 00:02:07,830 --> 00:02:11,225 We're going to loop through all of these people. 30 00:02:11,225 --> 00:02:14,705 For each of them, we're going to print out something like that, 31 00:02:14,705 --> 00:02:17,870 like what we did for Rodney Dangerfield. 32 00:02:17,870 --> 00:02:19,789 So, " Hello Rodney Dangerfield.", 33 00:02:19,789 --> 00:02:21,890 "Hello Marlon Brando", and "Hello you." 34 00:02:21,890 --> 00:02:23,210 What I'm going to do next, 35 00:02:23,210 --> 00:02:24,770 is show you something that's going to 36 00:02:24,770 --> 00:02:30,675 replace that concatenation with something that's a little cleaner. 37 00:02:30,675 --> 00:02:38,190 So, I've just replaced line five with something that is a little cleaner. 38 00:02:38,190 --> 00:02:42,160 We have a first part of it which is called the format string, 39 00:02:42,160 --> 00:02:47,845 and it's got some things in it which are places where we're going to do substitutions. 40 00:02:47,845 --> 00:02:51,520 So, we have a complete format string and you can look at that 41 00:02:51,520 --> 00:02:56,110 and get a sense of what the shape is of the thing that's going to come out at the end, 42 00:02:56,110 --> 00:02:58,420 but we're going to have some substitutions. 43 00:02:58,420 --> 00:03:00,910 This first set of curly braces says, 44 00:03:00,910 --> 00:03:04,390 do a substitution here and we have positional replacements. 45 00:03:04,390 --> 00:03:08,230 So, the first value is going to 46 00:03:08,230 --> 00:03:13,580 get stuck in there and the second value is going to get stuck in here. 47 00:03:14,130 --> 00:03:22,840 So, looking at this, we have a format string and then we are calling a method on it, 48 00:03:22,840 --> 00:03:25,265 the dot format method. 49 00:03:25,265 --> 00:03:28,355 So, it's a method on that string, 50 00:03:28,355 --> 00:03:31,910 and we're passing in a couple of parameter values. 51 00:03:31,910 --> 00:03:36,050 The first parameter value is going to get substituted in for the first curly braces, 52 00:03:36,050 --> 00:03:39,400 the second parameter value for the second curly braces. 53 00:03:39,400 --> 00:03:41,515 So, when I run this, 54 00:03:41,515 --> 00:03:43,685 the first time we get to line five, 55 00:03:43,685 --> 00:03:46,735 name will be Rodney Dangerfield and score will be minus one. 56 00:03:46,735 --> 00:03:50,125 That's what's going to get substituted into the hello string, 57 00:03:50,125 --> 00:03:54,230 and the next time it will be Marlon Brando and so on. 58 00:03:54,590 --> 00:04:00,700 So, we get the same result as before but our code is a little more readable. 59 00:04:00,700 --> 00:04:05,200 We'll be doing this more and more as the specialization goes on using 60 00:04:05,200 --> 00:04:10,810 these format strings rather than concatenating together strings out of individual parts. 61 00:04:10,810 --> 00:04:14,350 So, a little vocabulary that we have here. 62 00:04:14,350 --> 00:04:21,080 We have the format string, we have parameters, 63 00:04:21,080 --> 00:04:24,580 and these are the parameter values that are getting passed in, 64 00:04:24,580 --> 00:04:30,714 and then we have references post to substitute in those parameters. 65 00:04:30,714 --> 00:04:35,890 Here's another example that's a little more complicated where we can get some hints to 66 00:04:35,890 --> 00:04:41,635 the format method telling it how many decimal places to use for numbers. 67 00:04:41,635 --> 00:04:48,230 In this program, we're first asking the user to type 68 00:04:48,230 --> 00:04:54,890 in an original price for an item and then a percentage that it's going to be discounted. 69 00:04:54,890 --> 00:05:00,140 We compute a new price from the original price and the discount and then we're going to 70 00:05:00,140 --> 00:05:05,795 just create a string and finally print it out. 71 00:05:05,795 --> 00:05:09,470 The string that we're going to create is we're going to have 72 00:05:09,470 --> 00:05:16,565 a whole sentence that includes some information about the original price, 73 00:05:16,565 --> 00:05:18,545 how much the discount was, 74 00:05:18,545 --> 00:05:19,925 and what the new price is, 75 00:05:19,925 --> 00:05:22,175 and those things are going to get substituted in. 76 00:05:22,175 --> 00:05:24,410 The original price will go here, 77 00:05:24,410 --> 00:05:28,355 the discount will go here and the new price will go here. 78 00:05:28,355 --> 00:05:37,040 The new thing that we're seeing here is this colon 0.2f. 79 00:05:37,040 --> 00:05:39,560 What that says is the thing that's going to get 80 00:05:39,560 --> 00:05:43,605 substituted in here is a floating point number, 81 00:05:43,605 --> 00:05:45,370 that's what the f is saying, 82 00:05:45,370 --> 00:05:51,695 and the period two is saying after the decimal point have two more digits. 83 00:05:51,695 --> 00:05:55,490 If I run this, let's see what it looks like, 84 00:05:55,490 --> 00:06:00,960 I'm going to have an original price of 89.99. 85 00:06:02,450 --> 00:06:08,160 We're going to get a 20 percent discount and that comes out 86 00:06:08,160 --> 00:06:14,205 saying that 89.99 discounted by 20 percent is 71.99. 87 00:06:14,205 --> 00:06:23,220 Now, it isn't the case that 20 percent off of 89.99 is exactly 71.99. 88 00:06:23,220 --> 00:06:26,290 It's actually a tiny bit less than that but 89 00:06:26,290 --> 00:06:32,200 the closest number that's in dollars and cents is $71.99. 90 00:06:32,200 --> 00:06:38,320 If I told the Python interpreter that I wanted four decimal points instead of two, 91 00:06:38,320 --> 00:06:41,934 then it would print it out with more precision. 92 00:06:41,934 --> 00:06:45,415 Let's take our 89.99 item, 93 00:06:45,415 --> 00:06:48,055 get a 20 percent discount, 94 00:06:48,055 --> 00:06:55,920 and now it turns out it's 71.9920. 95 00:06:55,920 --> 00:06:59,080 So, we can control how many numbers we get after 96 00:06:59,080 --> 00:07:05,830 the decimal place by specifying it inside the curly braces. 97 00:07:05,830 --> 00:07:12,610 If we remove the hint entirely about how many decimal places to use, 98 00:07:12,610 --> 00:07:15,890 then it just goes and uses its default. 99 00:07:22,790 --> 00:07:27,505 In this case, it's giving us three decimal places. 100 00:07:27,505 --> 00:07:30,350 If we had some different value it might choose to give 101 00:07:30,350 --> 00:07:33,140 us more decimal places or less because we 102 00:07:33,140 --> 00:07:35,510 weren't giving it any instructions on how many did do 103 00:07:35,510 --> 00:07:39,155 is using its built-in algorithm to decide it. 104 00:07:39,155 --> 00:07:41,975 If we want to control exactly how it comes out, 105 00:07:41,975 --> 00:07:45,300 that's when we use this little hint. 106 00:07:51,530 --> 00:07:54,785 For dollars and cents, it really makes sense to have 107 00:07:54,785 --> 00:07:59,860 two decimal places because that's the precision for dollars and cents. 108 00:07:59,860 --> 00:08:02,490 All right. So, that's the format statement. 109 00:08:02,490 --> 00:08:10,330 Just a reminder, a format statement begins with a format string, 110 00:08:13,970 --> 00:08:17,900 and then we call the dot format method on it, 111 00:08:17,900 --> 00:08:21,820 and we pass in any values that are supposed to get substituted. 112 00:08:21,820 --> 00:08:29,220 The substitutions happen where the curly braces are. I'll see you next time.