1 00:00:00,910 --> 00:00:02,790 - [Instructor] In this video, I'd like to take a look 2 00:00:02,790 --> 00:00:06,450 at the String Types Format Method. 3 00:00:06,450 --> 00:00:10,600 Now, the F-strings or format strings that we've been using 4 00:00:10,600 --> 00:00:12,700 throughout these videos so far, 5 00:00:12,700 --> 00:00:15,100 were added to the Python language back 6 00:00:15,100 --> 00:00:17,400 in Python version 3.6. 7 00:00:17,400 --> 00:00:18,370 As you can see here, 8 00:00:18,370 --> 00:00:22,470 I'm currently using Python version 3.7.3. 9 00:00:22,470 --> 00:00:27,060 And we do use the F-strings throughout our videos and books. 10 00:00:27,060 --> 00:00:31,800 However, you will likely encounter older Python code 11 00:00:31,800 --> 00:00:33,820 that uses the format methods. 12 00:00:33,820 --> 00:00:37,860 So I did wanna make sure that I present that to you as well. 13 00:00:37,860 --> 00:00:39,710 Now the way the format method works 14 00:00:39,710 --> 00:00:42,250 is you create a format string, 15 00:00:42,250 --> 00:00:44,980 and then you invoke the format method 16 00:00:44,980 --> 00:00:48,800 on that string passing as arguments to the method, 17 00:00:48,800 --> 00:00:51,640 the values that should be inserted. 18 00:00:51,640 --> 00:00:53,300 So for example, let me go ahead 19 00:00:53,300 --> 00:00:55,830 and paste in a sample snippet. 20 00:00:55,830 --> 00:00:58,248 Here you can see we have a format string 21 00:00:58,248 --> 00:01:01,130 and in this format string the specifiers says 22 00:01:01,130 --> 00:01:03,740 that we would like to format a value 23 00:01:03,740 --> 00:01:05,730 as a floating point number with two digits. 24 00:01:05,730 --> 00:01:07,420 So the right of the decimal point, 25 00:01:07,420 --> 00:01:11,360 we're invoking on that format string, the format method 26 00:01:11,360 --> 00:01:13,430 and the value that we'd like to format 27 00:01:13,430 --> 00:01:16,540 in this case is 17.489. 28 00:01:16,540 --> 00:01:20,430 So as you can see, we do indeed get two digits to the right 29 00:01:20,430 --> 00:01:23,120 of the decimal point in the resulting strength. 30 00:01:23,120 --> 00:01:26,430 Now, you may be wondering what if there are multiple values 31 00:01:26,430 --> 00:01:27,530 to be inserted? 32 00:01:27,530 --> 00:01:31,840 Well, you can have multiple curly braced placeholders 33 00:01:31,840 --> 00:01:35,730 in your format string, and you can supply multiple arguments 34 00:01:35,730 --> 00:01:37,250 to the format method. 35 00:01:37,250 --> 00:01:41,700 So here is a simple snippet, in which all we wanna do 36 00:01:41,700 --> 00:01:45,530 is to insert the values Amanda and Cyan 37 00:01:45,530 --> 00:01:48,530 into the two placeholders in the format string, 38 00:01:48,530 --> 00:01:50,650 and they will be inserted in the order 39 00:01:50,650 --> 00:01:52,880 that they're specified by default. 40 00:01:52,880 --> 00:01:56,530 So as you can see, we get Amanda and then Cyan. 41 00:01:56,530 --> 00:02:00,680 Now when you're dealing with internationalization 42 00:02:00,680 --> 00:02:04,030 and localization, you might find it handy 43 00:02:04,030 --> 00:02:08,500 to reference arguments in the format methods parentheses 44 00:02:08,500 --> 00:02:10,280 by their position numbers, 45 00:02:10,280 --> 00:02:13,260 and then use different format strings 46 00:02:13,260 --> 00:02:16,340 depending on the language that you're working with. 47 00:02:16,340 --> 00:02:21,340 So here's an example, in which we have three placeholders, 48 00:02:21,440 --> 00:02:24,470 two of which have the same index number in them, 49 00:02:24,470 --> 00:02:26,550 and one of which does not. 50 00:02:26,550 --> 00:02:29,110 And as you can see, we only have two arguments, 51 00:02:29,110 --> 00:02:33,850 but the argument at index zero, the first argument to format 52 00:02:33,850 --> 00:02:37,170 will be placed twice into this format string. 53 00:02:37,170 --> 00:02:39,850 And the argument in the second position, 54 00:02:39,850 --> 00:02:44,050 index number one will be placed once into the format string. 55 00:02:44,050 --> 00:02:47,020 So as you can see, we get Happy Happy Birthday 56 00:02:47,020 --> 00:02:48,750 as a result of that. 57 00:02:48,750 --> 00:02:53,160 You can also use keyword arguments as well, 58 00:02:53,160 --> 00:02:55,710 which can make these format strings more readable. 59 00:02:55,710 --> 00:02:58,650 So for example, here we have a format string 60 00:02:58,650 --> 00:03:01,020 in which we've defined keywords first 61 00:03:01,020 --> 00:03:04,420 and last as the names of those placeholders. 62 00:03:04,420 --> 00:03:06,200 And then in the argument list, 63 00:03:06,200 --> 00:03:07,920 we can say first equals Amanda 64 00:03:07,920 --> 00:03:10,060 and that value will be placed here. 65 00:03:10,060 --> 00:03:12,090 And then we can say last equals Gray 66 00:03:12,090 --> 00:03:14,170 and that value will be placed here. 67 00:03:14,170 --> 00:03:17,170 And perhaps more interesting is the fact 68 00:03:17,170 --> 00:03:20,180 that of course, you can change the order. 69 00:03:20,180 --> 00:03:24,540 So last and first, same arguments first and last. 70 00:03:24,540 --> 00:03:27,020 But now they come out in reverse order 71 00:03:27,020 --> 00:03:31,420 because by the name it's inserted into the correct 72 00:03:31,420 --> 00:03:33,883 placeholder in the format string.