WEBVTT 1 00:00:00.146 --> 00:00:02.290 Hi guys and welcome back. 2 00:00:02.290 --> 00:00:05.820 In this lecture, we're going to look at string formatting. 3 00:00:05.820 --> 00:00:08.910 In the last video, we saw how you cannot add 4 00:00:08.910 --> 00:00:11.050 numbers and strings together, 5 00:00:11.050 --> 00:00:13.410 but that is such a common thing to do 6 00:00:13.410 --> 00:00:16.950 that all too often we end up having to convert the number 7 00:00:16.950 --> 00:00:20.230 into a string so that we can add it to the string and so on. 8 00:00:20.230 --> 00:00:21.543 Something like this, 9 00:00:22.770 --> 00:00:24.140 which we had in the last video. 10 00:00:24.140 --> 00:00:25.907 We've got a number, then we wanna put it 11 00:00:25.907 --> 00:00:28.462 inside the strings so that we can show it to the user. 12 00:00:28.462 --> 00:00:31.370 And we have to convert it into a string first. 13 00:00:31.370 --> 00:00:33.960 This is quite annoying because it happens so often. 14 00:00:33.960 --> 00:00:37.230 So instead, we can use string formatting. 15 00:00:37.230 --> 00:00:40.010 So what I'm going to do is instead of printing 16 00:00:40.010 --> 00:00:42.966 you are plus the age, 17 00:00:42.966 --> 00:00:47.180 we're gonna print f-string. 18 00:00:47.180 --> 00:00:49.580 Now an f-string is only something available 19 00:00:49.580 --> 00:00:52.000 in Python 3.6 and above, 20 00:00:52.000 --> 00:00:53.720 so make sure that you are using that version. 21 00:00:53.720 --> 00:00:55.210 And if you don't know what I'm talking about, 22 00:00:55.210 --> 00:00:57.403 don't worry, it will work in Repl.it. 23 00:00:58.360 --> 00:01:00.990 And here we're going to do you are 24 00:01:00.990 --> 00:01:02.790 and then inside a couple of curly braces, 25 00:01:02.790 --> 00:01:05.110 we're going to type age. 26 00:01:05.110 --> 00:01:06.850 Now we can delete this one here 27 00:01:06.850 --> 00:01:10.300 and we can also delete this one there. 28 00:01:10.300 --> 00:01:15.300 And we can directly print the number 34 inside our string, 29 00:01:15.330 --> 00:01:19.000 because of the way the f-strings work. 30 00:01:19.000 --> 00:01:21.860 So in an f-string, you type an f, 31 00:01:21.860 --> 00:01:23.723 then the quotation marks. 32 00:01:24.740 --> 00:01:26.620 And then inside the quotation marks 33 00:01:26.620 --> 00:01:28.920 you can write these curly braces 34 00:01:28.920 --> 00:01:31.550 and inside the curly brace you can write your variable 35 00:01:31.550 --> 00:01:34.320 that you want to include inside your string. 36 00:01:34.320 --> 00:01:35.820 Let's look at another example. 37 00:01:40.410 --> 00:01:44.220 Here we've got a name and then you've got a greeting 38 00:01:44.220 --> 00:01:45.300 which is an f-string. 39 00:01:45.300 --> 00:01:48.050 You see the f, then the quotation marks, 40 00:01:48.050 --> 00:01:51.350 and inside it you've got how are you, comma, space, 41 00:01:51.350 --> 00:01:53.350 then you've got your curly braces 42 00:01:53.350 --> 00:01:56.250 and inside there you've got the name of variable 43 00:01:56.250 --> 00:01:58.470 and at the end you've got a question mark. 44 00:01:58.470 --> 00:02:01.680 If we print this greeting and then we run this, 45 00:02:01.680 --> 00:02:05.440 you'll see that how are you, Jose?, gets printed out. 46 00:02:05.440 --> 00:02:07.680 It is a small problem with "f" strings 47 00:02:07.680 --> 00:02:10.340 which isn't really a problem as much as a limitation 48 00:02:10.340 --> 00:02:14.080 which is that if you change the name say to Bob 49 00:02:14.080 --> 00:02:15.853 and then you print greeting again, 50 00:02:17.140 --> 00:02:21.280 you'll see that how are you, Jose?, gets printed out twice. 51 00:02:21.280 --> 00:02:25.120 That is because when we calculated the string, 52 00:02:25.120 --> 00:02:28.050 the name variable was Jose 53 00:02:28.050 --> 00:02:31.490 and that is what the greeting string contains. 54 00:02:31.490 --> 00:02:33.860 The greeting string contains how are you, Jose? 55 00:02:33.860 --> 00:02:37.193 and even if you change name, the greeting doesn't change. 56 00:02:38.040 --> 00:02:41.440 That is why Python has another way of formatting strings 57 00:02:41.440 --> 00:02:45.520 which does allow for the changing of variables. 58 00:02:45.520 --> 00:02:50.070 So, let's delete these and create a final greeting 59 00:02:50.070 --> 00:02:52.340 which is how are you 60 00:02:52.340 --> 00:02:56.400 and then just opening and closing curly braces 61 00:02:56.400 --> 00:02:59.470 without a name of variable inside it 62 00:02:59.470 --> 00:03:01.563 and notice that this is not an f-string. 63 00:03:02.610 --> 00:03:04.410 Now we can do another variable 64 00:03:04.410 --> 00:03:08.200 that is final greeting dot format 65 00:03:08.200 --> 00:03:09.690 and pass in the name. 66 00:03:09.690 --> 00:03:10.880 So this is a little bit different 67 00:03:10.880 --> 00:03:12.700 from what we've been seeing up to now. 68 00:03:12.700 --> 00:03:16.200 What this does is it takes in this string here, 69 00:03:16.200 --> 00:03:20.110 how are you comma curly braces question mark 70 00:03:20.110 --> 00:03:22.980 and then, it's going to run 71 00:03:22.980 --> 00:03:25.500 something called format inside it 72 00:03:25.500 --> 00:03:28.370 and format has these brackets at the end 73 00:03:28.370 --> 00:03:30.570 which means they can accept a value 74 00:03:30.570 --> 00:03:33.360 and the value is accepting is the name. 75 00:03:33.360 --> 00:03:35.940 What this does is it takes the name 76 00:03:35.940 --> 00:03:39.718 and it replaces the curly braces by the value. 77 00:03:39.718 --> 00:03:43.480 So, if we print jose greeting, 78 00:03:43.480 --> 00:03:47.030 then you'll see the same thing as before, how are you, Jose? 79 00:03:47.030 --> 00:03:50.790 But now you can do something like bob greeting 80 00:03:50.790 --> 00:03:53.110 and make it equal to final greeting dot format 81 00:03:54.030 --> 00:03:58.110 of the name Bob and then we can print that out. 82 00:03:58.110 --> 00:04:01.853 So, as you can see, if we change the name to Bob, 83 00:04:02.890 --> 00:04:06.040 then this line actually looks exactly the same 84 00:04:06.040 --> 00:04:10.340 as the one above, but the name is changing. 85 00:04:10.340 --> 00:04:13.870 So, what we've got here is essentially template 86 00:04:13.870 --> 00:04:17.880 for the greeting and we can then replace the value into it 87 00:04:17.880 --> 00:04:19.600 later on if we want. 88 00:04:19.600 --> 00:04:21.350 Inside final greeting here, 89 00:04:21.350 --> 00:04:23.740 you can also put name if you want, 90 00:04:23.740 --> 00:04:25.890 but because it's not an f-string, 91 00:04:25.890 --> 00:04:28.660 Python isn't going to try to put your variable 92 00:04:28.660 --> 00:04:29.773 inside the string. 93 00:04:30.770 --> 00:04:34.520 You must do it yourself, but if you put this in there, 94 00:04:34.520 --> 00:04:35.710 what you have to do is 95 00:04:36.880 --> 00:04:40.550 final greeting dot format 96 00:04:40.550 --> 00:04:44.403 and now say name equal Jose. 97 00:04:47.160 --> 00:04:51.270 So what this does in Python is Python will know 98 00:04:51.270 --> 00:04:55.950 to look for the name thing inside curly braces 99 00:04:56.800 --> 00:04:59.093 and replace it by Jose. 100 00:05:02.260 --> 00:05:04.960 So this is a very important part of how Python works 101 00:05:04.960 --> 00:05:08.610 and we will look on it in greater detail as we go along. 102 00:05:08.610 --> 00:05:12.040 But the first part before the equal, 103 00:05:12.040 --> 00:05:15.250 Python will know that is this thing here 104 00:05:15.250 --> 00:05:18.713 and the second part is what you want to put there, instead. 105 00:05:19.740 --> 00:05:22.550 Now here comes the confusing part which is that 106 00:05:22.550 --> 00:05:24.480 you have a variable called name, 107 00:05:24.480 --> 00:05:27.730 so really you can do name equal name. 108 00:05:27.730 --> 00:05:30.910 But these to refer to two completely different things. 109 00:05:30.910 --> 00:05:33.530 The first one, Python knows you refer to 110 00:05:33.530 --> 00:05:36.260 this thing here inside your greeting 111 00:05:36.260 --> 00:05:37.990 and the second one after the equal, 112 00:05:37.990 --> 00:05:40.323 Python knows that you refer to your variable. 113 00:05:41.510 --> 00:05:44.050 Don't feel like you have to use this all the time, 114 00:05:44.050 --> 00:05:46.890 but it is there and you can use it if you want. 115 00:05:46.890 --> 00:05:49.220 Well, the key benefits of doing something like this 116 00:05:49.220 --> 00:05:52.220 is that the template is now much more readable, 117 00:05:52.220 --> 00:05:55.000 because it is obvious that you are going to print, 118 00:05:55.000 --> 00:05:57.630 how are you and then somebody's name. 119 00:05:57.630 --> 00:06:00.240 In addition, it of course, works 120 00:06:00.240 --> 00:06:02.270 if you change this variable name. 121 00:06:02.270 --> 00:06:05.210 Name which is still being used inside the string 122 00:06:05.210 --> 00:06:09.300 is now equal to friend_name which is our variable, 123 00:06:09.300 --> 00:06:12.120 so this still look works in the same way. 124 00:06:12.120 --> 00:06:13.313 Let's revert to that. 125 00:06:14.980 --> 00:06:19.090 And now remember that name is going to be replacing 126 00:06:19.090 --> 00:06:20.530 just the curly braces, 127 00:06:20.530 --> 00:06:23.510 because we don't have anything inside them 128 00:06:23.510 --> 00:06:26.623 which is when we need to say name equal something. 129 00:06:28.000 --> 00:06:30.820 Instead of creating a variable called jose greeting, 130 00:06:30.820 --> 00:06:33.760 you can actually just put this inside the print function 131 00:06:33.760 --> 00:06:35.880 and that is totally fine as well. 132 00:06:35.880 --> 00:06:37.700 It's a little bit more confusing maybe, 133 00:06:37.700 --> 00:06:39.873 but it works in the same way. 134 00:06:40.830 --> 00:06:43.140 Usually, you will be using f-strings in Python 135 00:06:43.140 --> 00:06:45.630 just because they are shorter, they're more readable, 136 00:06:45.630 --> 00:06:48.630 you don't have to type like dot format and then brackets 137 00:06:48.630 --> 00:06:50.220 and then pass in things there. 138 00:06:50.220 --> 00:06:52.950 So, f-strings are the weapon of choice. 139 00:06:52.950 --> 00:06:55.860 But sometimes when you want to reuse a template 140 00:06:55.860 --> 00:06:57.840 using format, comes in handy. 141 00:06:57.840 --> 00:06:58.890 But that's it for this video, 142 00:06:58.890 --> 00:07:01.180 I hopefully I haven't confused you too much. 143 00:07:01.180 --> 00:07:04.080 Thank you for joining me and I'll see you in the next one.