WEBVTT 1 00:00:01.991 --> 00:00:06.320 In this video, we're gonna start talking about string formating in Python. 2 00:00:06.320 --> 00:00:09.800 Now when printing strings and numbers, it's often gonna be convenient to display 3 00:00:09.800 --> 00:00:15.060 both strings and numbers at the same time, in a single call to print. 4 00:00:15.060 --> 00:00:17.840 So, for example you might want to print a description of what the value 5 00:00:17.840 --> 00:00:19.640 is before the value itself. 6 00:00:19.640 --> 00:00:23.645 As you saw, in a previous video, numbers cannot be canonated or 7 00:00:23.645 --> 00:00:26.900 appended with strings using the plus operator, because 8 00:00:26.900 --> 00:00:30.390 the presence of that number instructs Python to actually to attempt addition. 9 00:00:30.390 --> 00:00:34.190 And we saw that actually crashes Python, because it can't figure out what to do. 10 00:00:34.190 --> 00:00:37.150 So there are a couple of, different approaches to doing this. 11 00:00:37.150 --> 00:00:40.360 So in this video I'm going to go through and actually show you some of those. 12 00:00:40.360 --> 00:00:44.310 So as you can see on the screen, I have created a new project. 13 00:00:44.310 --> 00:00:47.510 A Python project in IntelliJ called string formating. 14 00:00:47.510 --> 00:00:52.530 I also created a Python file using the new command in IntelliJ so 15 00:00:52.530 --> 00:00:55.080 that we can actually start typing our code in. 16 00:00:55.080 --> 00:00:57.920 So lets look at one way to actually do what I suggested, 17 00:00:57.920 --> 00:01:02.850 which is to actually display a string and a number in the one print statement. 18 00:01:02.850 --> 00:01:06.620 We're gonna use something called the string function, 19 00:01:06.620 --> 00:01:08.170 or str as it's correctly known. 20 00:01:08.170 --> 00:01:09.990 So let's use that now. 21 00:01:09.990 --> 00:01:11.806 Gonna type age = 24. 22 00:01:11.806 --> 00:01:16.499 And I'm gonna do printMy age 23 00:01:16.499 --> 00:01:20.770 is +, now I'm gonna do it the old way we 24 00:01:20.770 --> 00:01:23.720 established in a previous video didn't work just to recap. 25 00:01:23.720 --> 00:01:26.580 So that method will actually result in Python crashing. 26 00:01:26.580 --> 00:01:28.490 So you can see on the right hand side there, and 27 00:01:28.490 --> 00:01:33.280 the reason it's crashing is it's actually trying to concatenate or append a number. 28 00:01:33.280 --> 00:01:35.920 We know that age is a number because of this line here and 29 00:01:35.920 --> 00:01:39.650 line two to a string, which of course is something in double quotes. 30 00:01:39.650 --> 00:01:40.970 We can use a method, 31 00:01:40.970 --> 00:01:44.960 which is something that's brought into Python called the STR method. 32 00:01:46.090 --> 00:01:50.230 And type STR and put a bracket around the age variable, like so. 33 00:01:50.230 --> 00:01:51.960 And effectively what that is doing, 34 00:01:51.960 --> 00:01:55.650 that line there is actually converting what's in the brackets to a string. 35 00:01:55.650 --> 00:01:58.940 So consequently when we run that, we no longer get an error and 36 00:01:58.940 --> 00:02:02.200 we actually get the correct result, my age is 24 years. 37 00:02:02.200 --> 00:02:07.100 So while other languages such as Java will do this for you automatically, Java would 38 00:02:07.100 --> 00:02:10.410 not have crashed in that scenario I showed you on the screen where Python did. 39 00:02:10.410 --> 00:02:11.300 In Python, 40 00:02:11.300 --> 00:02:14.450 we actually need to do it specifically and this is one way of doing it. 41 00:02:14.450 --> 00:02:17.660 Now, you can probably imagine if you had four, five, 42 00:02:17.660 --> 00:02:22.420 or more numbers to actually print like this that it could quickly rather tedious 43 00:02:22.420 --> 00:02:24.180 to having to type that every time. 44 00:02:24.180 --> 00:02:27.660 I'm trying to coerce every variable using the STR method. 45 00:02:27.660 --> 00:02:31.425 And fortunately for us, Python actually provides a much better mechanism. 46 00:02:31.425 --> 00:02:34.960 Now what I'm gonna do is, I'm gonna show you something in Python called replacement 47 00:02:34.960 --> 00:02:38.190 fields and this is gonna be a way that makes it a lot easier to make changes like 48 00:02:38.190 --> 00:02:41.440 this, when your dealing with multiple numbers on the one line. 49 00:02:41.440 --> 00:02:43.650 We're gonna do something called replacement fields. 50 00:02:43.650 --> 00:02:46.790 And later on I'm gonna start talking about an old method 51 00:02:46.790 --> 00:02:50.750 which was called a formating operator, and we'll understand why shortly. 52 00:02:50.750 --> 00:02:53.760 But for now, let's just get stuck into showing you how to do this with 53 00:02:53.760 --> 00:02:54.975 a replacement field. 54 00:02:54.975 --> 00:02:58.235 And probably the best way to actually explain it is actually to show 55 00:02:58.235 --> 00:02:59.315 you an example of it. 56 00:02:59.315 --> 00:03:02.855 So in the scenario of printing as we did above we could do 57 00:03:02.855 --> 00:03:06.105 what is equivalent using these string str method. 58 00:03:06.105 --> 00:03:11.135 We can type, My age is, and 59 00:03:11.135 --> 00:03:16.295 notice I put curly brackets and a number, the number zero in between them. 60 00:03:16.295 --> 00:03:21.507 And I've typed years dot, so type .format(age). 61 00:03:21.507 --> 00:03:25.710 So what I've done there, this is called a replacement field and 62 00:03:25.710 --> 00:03:28.330 this is the actual replacement for this part here. 63 00:03:28.330 --> 00:03:33.210 So it's the syntax of it is a curly brace at the start, 64 00:03:33.210 --> 00:03:36.310 a number in the middle and another curly brace to actually end it. 65 00:03:36.310 --> 00:03:38.390 And that number, which we'll talk about more shortly, 66 00:03:38.390 --> 00:03:41.370 comes into play when we're actually dealing with multiple parameters. 67 00:03:41.370 --> 00:03:42.730 But we'll talk more about that in a minute. 68 00:03:42.730 --> 00:03:44.210 But now let's just run this example, 69 00:03:44.210 --> 00:03:46.620 and we should get exactly the same result as we got before. 70 00:03:46.620 --> 00:03:50.220 Now it doesn't look to be a particularly better way of doing things at the moment, 71 00:03:50.220 --> 00:03:52.710 but where it really starts coming in handy, 72 00:03:52.710 --> 00:03:55.210 is if we've actually got a lot of data and a lot of parameters. 73 00:03:55.210 --> 00:03:58.750 So what i'm gonna do is I'm gonna paste in a bit of code here and 74 00:03:58.750 --> 00:04:01.200 I'm going to just actually make it so it goes up to two lines so 75 00:04:01.200 --> 00:04:02.520 we can actually see it a bit better. 76 00:04:04.630 --> 00:04:09.450 Like so, I'll just move this over a little bit. 77 00:04:09.450 --> 00:04:12.410 So what we've got is, now we've got one big string as you can see there, 78 00:04:12.410 --> 00:04:14.220 that's just all one big string. 79 00:04:14.220 --> 00:04:16.630 But notice how we've got multiple replacement fields now. 80 00:04:16.630 --> 00:04:20.340 So we've got the zero, the one, the, two, the three, the four, the five, 81 00:04:20.340 --> 00:04:21.710 the six, and the seven. 82 00:04:21.710 --> 00:04:24.750 And in the past, in the previous example we had doc format and 83 00:04:24.750 --> 00:04:28.230 we only had just the one thing, the one bit of information which was age, and 84 00:04:28.230 --> 00:04:32.170 that went automatically into this position here, into have a replacement for zero. 85 00:04:32.170 --> 00:04:35.990 Now, we've actually got a total, if you count them, one, two, three, four, five, 86 00:04:35.990 --> 00:04:37.390 six, seven, eight. 87 00:04:37.390 --> 00:04:41.410 And they're actually numbered from zero to seven within the string. 88 00:04:41.410 --> 00:04:42.800 So, what's gonna happen here is, 89 00:04:42.800 --> 00:04:47.090 each one of these is going to be matched up into the particular placement fields. 90 00:04:47.090 --> 00:04:49.370 So, 31 should go into where it's got zero. 91 00:04:49.370 --> 00:04:53.390 The replacement through zero generate into number one, march into number two, 92 00:04:53.390 --> 00:04:54.030 and so on. 93 00:04:54.030 --> 00:04:58.090 And when we actually run this, we should actually see that. 94 00:04:58.090 --> 00:05:00.510 We're gonna open this out so we can see the result. 95 00:05:00.510 --> 00:05:04.590 There are 31 days in January, March, May, July, August, October, December. 96 00:05:04.590 --> 00:05:09.837 So I can see what happened, it actually took those values It took the values in 97 00:05:09.837 --> 00:05:14.200 the .format part of this line and actually put them automatically in the right spot. 98 00:05:14.200 --> 00:05:18.430 So that's the concept of a replacement field as you can see, that it does 99 00:05:18.430 --> 00:05:21.925 really make it a lot easier from the point of view of putting your data together, 100 00:05:21.925 --> 00:05:25.310 cuz we're putting all of the data that we want outputted in the one place. 101 00:05:25.310 --> 00:05:28.590 We're not having to put a bid here like putting your plus str and 102 00:05:28.590 --> 00:05:31.460 whatever the thing is that you want to actually add to that spot as well. 103 00:05:31.460 --> 00:05:34.240 So it's a lot easier to actually process and work with. 104 00:05:34.240 --> 00:05:37.530 That's the one, the replacement fields is the new way of doing things. 105 00:05:37.530 --> 00:05:40.590 And with that said, we can actually make this better as well, 106 00:05:40.590 --> 00:05:42.810 and there's nothing to stop us from using triple quotes. 107 00:05:42.810 --> 00:05:44.810 So for example, I can come back here and 108 00:05:44.810 --> 00:05:48.600 I can paste in some more code and notice I'm using the triple quotes again so 109 00:05:48.600 --> 00:05:51.090 that we're actually dealing with data on multiple lines. 110 00:05:51.090 --> 00:05:55.690 And notice also, I'm reusing a replacement field more than once. 111 00:05:55.690 --> 00:05:59.530 So number two you can see is used multiple times as is number one. 112 00:05:59.530 --> 00:06:02.900 And number zero is also used once as well. 113 00:06:02.900 --> 00:06:07.490 So now when we run that example you can see we've got some data there. 114 00:06:07.490 --> 00:06:08.050 What I might do is, 115 00:06:08.050 --> 00:06:10.810 I might leave this window and then run one window down to the bottom of the screen so 116 00:06:10.810 --> 00:06:12.060 that we can see things a little bit better. 117 00:06:13.330 --> 00:06:13.940 Move to bottom. 118 00:06:13.940 --> 00:06:16.600 It'd be a little bit easier for us to actually read. 119 00:06:16.600 --> 00:06:20.230 So, I'm just going to leave a little bit of space there. 120 00:06:20.230 --> 00:06:23.330 So you can see that it handles quite nicely using the triple quotes 121 00:06:23.330 --> 00:06:28.005 to deal with strings of handling that go over to more than line, 122 00:06:28.005 --> 00:06:29.810 multi-lines in other words. 123 00:06:29.810 --> 00:06:33.380 And obviously compared to the approach at the top of the line three. 124 00:06:33.380 --> 00:06:36.520 That would be a huge long line to actually have to type in if you were doing 125 00:06:36.520 --> 00:06:38.790 like that and it would become quite messy. 126 00:06:38.790 --> 00:06:42.150 So it's a great way to actually replace things and do things in a better way. 127 00:06:42.150 --> 00:06:44.620 And as I said that's being introduced into Python 3. 128 00:06:44.620 --> 00:06:47.120 Now there is another way of doing things and 129 00:06:47.120 --> 00:06:49.230 this is called the string formatting operator. 130 00:06:49.230 --> 00:06:52.890 I mentioned that briefly before and I'm gonna show you how to use it, 131 00:06:52.890 --> 00:06:57.260 despite the fact that it's now being deprecated in Python 3. 132 00:06:57.260 --> 00:06:58.350 And when I say deprecate, 133 00:06:58.350 --> 00:07:02.980 what that actually means is that's a feature that's a part of the program that 134 00:07:02.980 --> 00:07:08.090 actually worked which is now no longer recommended to be used in Python 3. 135 00:07:08.090 --> 00:07:12.160 So it's introduce into Python 2, and it's used extensively in Python 2 code. 136 00:07:12.160 --> 00:07:16.510 And actually that's the reason why I'm going to actually teach you some of this, 137 00:07:16.510 --> 00:07:20.500 because the chances are gonna be high as you move forward learning Python, 138 00:07:20.500 --> 00:07:24.330 that you'll be getting access to Python 2 code. 139 00:07:24.330 --> 00:07:28.550 Because Python 2 has been out there for so long, there's just a huge amount of code 140 00:07:28.550 --> 00:07:32.030 out there and it's kinda pretty important for you to actually see that and 141 00:07:32.030 --> 00:07:34.830 understand how that works because it's a fundamental pile for so 142 00:07:34.830 --> 00:07:36.550 many Python 2 programs. 143 00:07:36.550 --> 00:07:39.610 The other thing you'll be able to do is once you see the way 144 00:07:39.610 --> 00:07:42.760 that this formatting operator works. 145 00:07:42.760 --> 00:07:43.570 If you decide to, 146 00:07:43.570 --> 00:07:47.870 you'll be able to actually convert that to the new replacement field syntax. 147 00:07:47.870 --> 00:07:51.300 So let's just start from the basics, and have a look how it works. 148 00:07:51.300 --> 00:07:56.840 So what I'm going to do is start by typing a formatting operator for 149 00:07:56.840 --> 00:07:58.280 our first example, which was the age. 150 00:07:58.280 --> 00:08:04.652 So print( My age is, and this time we're doing it a little bit different, 151 00:08:04.652 --> 00:08:10.564 we put %D years, And a percent sign again and then whatever we want printed, 152 00:08:10.564 --> 00:08:13.540 in this case, it would be the age variable that we defined in line two. 153 00:08:13.540 --> 00:08:16.800 Now that actually should have been a lowercase d. 154 00:08:16.800 --> 00:08:18.950 Like so, it does actually make a difference. 155 00:08:18.950 --> 00:08:22.770 So now if I run that we should find below all those dates we should get the same 156 00:08:22.770 --> 00:08:25.700 answer that we got when we printed the example from line two. 157 00:08:25.700 --> 00:08:29.680 And you can see that it still does come up and says My age is 24 years. 158 00:08:29.680 --> 00:08:31.540 This again is the old format. 159 00:08:31.540 --> 00:08:34.970 So what actually happened here is you can see 160 00:08:34.970 --> 00:08:37.740 the %d actually got replaced with an integer. 161 00:08:37.740 --> 00:08:41.390 In the integer of course was a value that was in the age variable. 162 00:08:41.390 --> 00:08:47.113 So taking it to the next level, we could actually do something like print, My age 163 00:08:47.113 --> 00:08:51.446 is %d, I can also put %s. 164 00:08:51.446 --> 00:08:58.340 %d, %s let me put a percent again. 165 00:08:58.340 --> 00:09:01.660 This time we put because we've got multiple things that we wanna print. 166 00:09:01.660 --> 00:09:06.395 We can print those in brackets, age, years, 167 00:09:06.395 --> 00:09:11.900 six, months. 168 00:09:11.900 --> 00:09:16.990 All right so, now if we actually run that you can see that it comes up and 169 00:09:16.990 --> 00:09:20.360 says, my age is 24 years, six months. 170 00:09:20.360 --> 00:09:23.930 And you can see what happens is, the first time it's come along, it's what my age is. 171 00:09:23.930 --> 00:09:27.385 It's looked at the variable age and so that needs to be 24 or 172 00:09:27.385 --> 00:09:29.595 in this case I just put a 24 in there. 173 00:09:29.595 --> 00:09:33.405 The next one was a %s, s stands for string. 174 00:09:33.405 --> 00:09:36.905 And so therefore let's put in the value, in this case the years, 175 00:09:36.905 --> 00:09:39.365 which was a literal string that we typed in. 176 00:09:39.365 --> 00:09:43.695 Put it in that spot and you can see that it's done the same thing for the six and 177 00:09:43.695 --> 00:09:44.945 the months as well. 178 00:09:44.945 --> 00:09:48.805 So you can actually have multiple replacements going on 179 00:09:48.805 --> 00:09:50.560 in the one print line, as you can see there. 180 00:09:50.560 --> 00:09:54.750 So replacement's strictly one value at a time, from left to right, and it's got 181 00:09:54.750 --> 00:09:59.400 pretty well none of the flexibility offered by Python 3 replacement fields. 182 00:09:59.400 --> 00:10:03.040 So you should really be focusing on that, replacement fields where possible. 183 00:10:03.040 --> 00:10:05.620 But again, I'm showing you this now just so you can look at 184 00:10:05.620 --> 00:10:09.520 type Python 2 code at some point if you need to, and understand how it works. 185 00:10:09.520 --> 00:10:12.790 Now just to take that a little bit further, let's look at another example and 186 00:10:12.790 --> 00:10:13.700 that's using the for loop. 187 00:10:13.700 --> 00:10:16.230 And we're going to use the for loop one more time, bearing 188 00:10:16.230 --> 00:10:20.040 in mind that we're going to cover the for loop officially in the next section. 189 00:10:20.040 --> 00:10:22.730 But for now, let's just focus on the print statement. 190 00:10:22.730 --> 00:10:27.607 So I'm going to type for, i, in range, 191 00:10:27.607 --> 00:10:33.498 one comma 12, colon, print, number %2d. 192 00:10:33.498 --> 00:10:39.982 And notice I put a number there this time, 193 00:10:39.982 --> 00:10:45.731 squared is %4d and cubed is %4d. 194 00:10:48.152 --> 00:10:53.029 Then for the answers I'm gonna put %i, i, star, star, two, and 195 00:10:53.029 --> 00:10:58.679 it sort of multiply in the previous video the two stars together that indicates 196 00:10:58.679 --> 00:11:04.892 that we're actually doing, we're trying to raise a number to the power of another. 197 00:11:04.892 --> 00:11:09.480 So i ** 2 in this case so i ** 2 is i squared. 198 00:11:09.480 --> 00:11:12.262 And the next one, I'm gonna do i **, or 199 00:11:12.262 --> 00:11:17.560 to the power of, three like so, gives us an answer that's i cubed. 200 00:11:17.560 --> 00:11:20.900 Now if we actually run this and actually have a look at the result 201 00:11:20.900 --> 00:11:24.590 you can see on the output, we've got things nicely formatted. 202 00:11:24.590 --> 00:11:27.760 So the for loop's gone through and done the processing for everything, and 203 00:11:27.760 --> 00:11:31.220 in terms of printing out the numbers, you can see how there's been 204 00:11:31.220 --> 00:11:33.790 a certain amount of width that's been allocated for each section. 205 00:11:33.790 --> 00:11:36.700 So even though it didn't need to use the letter, or 206 00:11:36.700 --> 00:11:41.180 the space before the number 1 We actually put the number in there, we said two, 207 00:11:41.180 --> 00:11:45.700 use up to two, or to allocate two spaces for this var actually working. 208 00:11:45.700 --> 00:11:49.970 Consequently, it allocated that space and just made it blank if it wasn't in use. 209 00:11:49.970 --> 00:11:54.090 And as a result you can see that things are nicely formatted and nicely in order. 210 00:11:54.090 --> 00:11:57.830 And just to confirm that, if we actually remove that and 211 00:11:57.830 --> 00:11:59.210 run this without those numbers in there. 212 00:11:59.210 --> 00:12:02.650 It does still work but it doesn't look anywhere near as neat. 213 00:12:02.650 --> 00:12:06.360 Yes, the first part does up until we've got a number that's greater than nine. 214 00:12:06.360 --> 00:12:09.870 Then you see it starts really losing the actually formatting quite badly. 215 00:12:09.870 --> 00:12:12.160 So I'm gonna put that back to how it was before again. 216 00:12:13.410 --> 00:12:16.670 Run it again so we've got some nice formatting going on and 217 00:12:16.670 --> 00:12:19.740 there's other types of operators you can actually do there. 218 00:12:19.740 --> 00:12:22.900 We've actually put %d, is the one that's for 219 00:12:22.900 --> 00:12:27.265 an integer and you saw %s before, s plus four a string. 220 00:12:27.265 --> 00:12:31.305 Now another thing you can actually do is you can actually specify the precision of 221 00:12:31.305 --> 00:12:32.305 a number as well. 222 00:12:32.305 --> 00:12:33.485 So let's go ahead and actually do that. 223 00:12:33.485 --> 00:12:38.679 And the way to do that, and we can talk for example print pi is 224 00:12:38.679 --> 00:12:46.430 approximately %12f. 225 00:12:46.430 --> 00:12:51.330 And if type 22 divided by 7, if you run that, you can 226 00:12:55.340 --> 00:12:59.370 see by default we've got 3.142857. 227 00:12:59.370 --> 00:13:01.770 And the calculation's actually six decimal places wide. 228 00:13:01.770 --> 00:13:03.070 But we can actually request more. 229 00:13:03.070 --> 00:13:05.480 So we could change the format stream, which we'll do now. 230 00:13:05.480 --> 00:13:06.610 Let's change that, say, to 50. 231 00:13:06.610 --> 00:13:12.940 So we'll come here where it's got 12f and we'll change that to 12.50. 232 00:13:12.940 --> 00:13:15.620 So we're saying that we want a lot more precision there. 233 00:13:15.620 --> 00:13:16.930 So if we do that and run that again. 234 00:13:18.040 --> 00:13:21.480 You can see, we've now got a number that literally has 50 decimal points. 235 00:13:21.480 --> 00:13:25.050 So it's far more detailed, far more precise than what the previous one was. 236 00:13:25.050 --> 00:13:28.170 And it's just adding these numbers in the various part of this, 237 00:13:28.170 --> 00:13:32.140 after the percent sign and before the actual type, which signifies and 238 00:13:32.140 --> 00:13:34.240 actually shows where that position is gonna be. 239 00:13:34.240 --> 00:13:35.860 Whether it's gonna be after the decimal point or 240 00:13:35.860 --> 00:13:39.250 whether it's sort of a gap, in the number to make sure that it lines up nicely. 241 00:13:39.250 --> 00:13:44.090 So once again, the thing to keep in mind is that you probably wont be using these 242 00:13:44.090 --> 00:13:46.980 and you shouldn't be using those in Python 3 generally. 243 00:13:46.980 --> 00:13:49.384 And this is the string formating operators I'm talking about, 244 00:13:49.384 --> 00:13:51.020 these with the percent signs. 245 00:13:51.020 --> 00:13:53.950 But it is important to understand them because you almost certainly will 246 00:13:53.950 --> 00:13:56.274 come across them in your Python codes. 247 00:13:56.274 --> 00:14:00.170 And what I've in the screen now, I'm gonna just open a browser and show you, 248 00:14:00.170 --> 00:14:04.430 this page here and there's a link to this page in the actual video itself. 249 00:14:04.430 --> 00:14:07.960 So go to the resources section of this video and you may have to come here and 250 00:14:07.960 --> 00:14:10.840 actually find out more about string formatting operators and 251 00:14:10.840 --> 00:14:14.490 how they apply to the older version or Python version 2. 252 00:14:14.490 --> 00:14:19.420 By getting back to replacement fields, what we've done here we can 253 00:14:19.420 --> 00:14:23.290 pretty well do it identically and more with replacement fields. 254 00:14:23.290 --> 00:14:25.250 So let's try another example. 255 00:14:25.250 --> 00:14:26.800 We're gonna copy the first part. 256 00:14:29.190 --> 00:14:32.880 Like so, and then we're gonna actually change it into, placed with some syntax. 257 00:14:32.880 --> 00:14:35.600 So I'm gonna type zero which we did before, but 258 00:14:35.600 --> 00:14:37.560 I'm gonna but a colon and a number. 259 00:14:37.560 --> 00:14:39.560 That's gonna be how wide. 260 00:14:39.560 --> 00:14:42.260 We're actually allocating space four. 261 00:14:42.260 --> 00:14:47.590 And likewise I can do 1:4 and 262 00:14:47.590 --> 00:14:50.670 cubed, 2:4. 263 00:14:50.670 --> 00:14:55.900 And instead of the percent which was of course the old way of doing things, 264 00:14:55.900 --> 00:14:58.510 we do type.format as we did earlier and 265 00:14:58.510 --> 00:15:01.200 then we just have the same parameters as they had before. 266 00:15:01.200 --> 00:15:04.490 So when we run that we should get an identical result but 267 00:15:04.490 --> 00:15:07.040 using the new replacement field syntax. 268 00:15:07.040 --> 00:15:09.910 You can see that we have everything nicely formated. 269 00:15:09.910 --> 00:15:12.230 Much as we had with the old approach but 270 00:15:12.230 --> 00:15:14.830 obviously using the new replacement field syntax. 271 00:15:14.830 --> 00:15:16.820 Now, that's what's specifying the width again. 272 00:15:16.820 --> 00:15:20.190 So just to be clear, the first one was the replacement for your number. 273 00:15:20.190 --> 00:15:22.680 And that's which value from 274 00:15:22.680 --> 00:15:26.640 this area after the .format is actually used in which spot. 275 00:15:26.640 --> 00:15:28.858 But the other part after that, of course, was the width. 276 00:15:28.858 --> 00:15:33.700 So colon two meant we allocated a width of 2:4, we allocated a width of four, and 277 00:15:33.700 --> 00:15:35.830 so on and so forth. 278 00:15:35.830 --> 00:15:38.090 Now we can also tidy them up a little bit though. 279 00:15:38.090 --> 00:15:42.130 So what we can do is we can ensure that numbers occupy the same field width but 280 00:15:42.130 --> 00:15:46.340 are left aligned by placing a less than sign after the symbol. 281 00:15:46.340 --> 00:15:50.450 So what we can do there is, come down here, we can leave the first one as it is, 282 00:15:50.450 --> 00:15:55.440 but the second one, with the squared result, we'll put less than four, 283 00:15:55.440 --> 00:15:58.240 down here to two we put less than four there. 284 00:15:58.240 --> 00:16:03.010 If we actually run that, notice how things are now left formatted. 285 00:16:03.010 --> 00:16:05.100 We're still actually allocating the same amount of space. 286 00:16:05.100 --> 00:16:09.040 So things are still lined up but effectively we're left justifying 287 00:16:09.040 --> 00:16:11.920 the numbers so the number will always start from the left hand side. 288 00:16:11.920 --> 00:16:13.920 That's just another way of doing things as well. 289 00:16:13.920 --> 00:16:18.240 And what we could do for the precision example coming back here. 290 00:16:18.240 --> 00:16:20.410 We could actually just copy the Pi result again. 291 00:16:20.410 --> 00:16:25.450 So I'll copy that line, put it down here and we could change that. 292 00:16:25.450 --> 00:16:29.970 Where we had %12f, we could actually delete that change it to replacement 293 00:16:29.970 --> 00:16:35.160 fields format 0:12 to add the precision .50. 294 00:16:35.160 --> 00:16:38.800 So you can see that what you could do with the old format in the new format 295 00:16:38.800 --> 00:16:42.070 using replacement fields quite easily, and change that of course to format. 296 00:16:42.070 --> 00:16:43.430 And there's only the one parameter, 297 00:16:43.430 --> 00:16:46.960 cuz it's only the one replacement field that we're using. 298 00:16:46.960 --> 00:16:50.430 And if we run that, we get the same result with pi, 299 00:16:50.430 --> 00:16:52.020 as you can see down on the bottom of the screen. 300 00:16:53.670 --> 00:16:56.700 So it's quite easy to convert the old style modifiers, 301 00:16:56.700 --> 00:17:00.880 as you saw me do in this video and replace and convert those into the new .format or 302 00:17:00.880 --> 00:17:02.650 the new replacement fields syntax. 303 00:17:02.650 --> 00:17:05.210 The other thing I wanted to mention, was two other things I wanted to mention 304 00:17:05.210 --> 00:17:08.055 before we end this section and I wanted to paste one example in here. 305 00:17:08.055 --> 00:17:10.395 I'm not sure whether we made it clear enough here, but 306 00:17:10.395 --> 00:17:12.565 we can actually do something like this. 307 00:17:12.565 --> 00:17:16.495 You see that we've used the actual replacement numbers out of order for 308 00:17:16.495 --> 00:17:17.645 the replacement fields. 309 00:17:17.645 --> 00:17:20.678 So it doesn't have to start from zero, one, 310 00:17:20.678 --> 00:17:24.405 two and unlike the actual percentage approach with these modifiers, 311 00:17:24.405 --> 00:17:26.825 we can only use those once, and they have to be in order. 312 00:17:26.825 --> 00:17:29.065 In this case we can reuse them multiple times. 313 00:17:29.065 --> 00:17:32.710 So you can see here that we're actually showing we're using a replacement field, 314 00:17:32.710 --> 00:17:35.130 like number two was used in multiple positions. 315 00:17:35.130 --> 00:17:37.280 That's one of the other reasons to use replacement fields. 316 00:17:37.280 --> 00:17:41.120 And of course if you run it, we'll see that it works quite nicely. 317 00:17:41.120 --> 00:17:42.090 Now just in finishing off, 318 00:17:42.090 --> 00:17:45.990 the other thing to note that in terms of those numbers, keep in mind that 319 00:17:45.990 --> 00:17:48.870 actually the use of numbers in replacement fields are actually optional. 320 00:17:48.870 --> 00:17:54.049 So we can actually do something like this if we prefer And 321 00:17:54.049 --> 00:17:56.571 you can see there I'm going through the same range again, but 322 00:17:56.571 --> 00:18:00.360 notice how I'm not actually inferring, or not actually typing in a number at all. 323 00:18:00.360 --> 00:18:03.800 So what will happen there is that will automatically assume that the first one 324 00:18:03.800 --> 00:18:07.310 you use will be zero, the second one will be one, and the third one will be two. 325 00:18:07.310 --> 00:18:08.880 So if we run that we'll see that. 326 00:18:10.110 --> 00:18:12.310 And you can see we still got the correct results. 327 00:18:13.580 --> 00:18:16.150 And note how the third one, even though we didn't specify a number, 328 00:18:16.150 --> 00:18:17.760 we can still actually control the layout. 329 00:18:17.760 --> 00:18:22.200 But of course in that scenario, what you couldn't do is use the value more than 330 00:18:22.200 --> 00:18:25.690 once, as we used in the example above, because of course we need to 331 00:18:25.690 --> 00:18:29.180 actually specify that number if we aregonna use it more than once. 332 00:18:29.180 --> 00:18:32.130 But in those situations where you don't need to actually use something more than 333 00:18:32.130 --> 00:18:34.220 once, this is probably gonna be ideal for you, and 334 00:18:34.220 --> 00:18:36.230 it's a little bit less to type as well. 335 00:18:36.230 --> 00:18:38.580 Okay, so that's it, that's actually the end of this section, so 336 00:18:38.580 --> 00:18:41.230 we've actually looked at running a simple hello world Python program. 337 00:18:41.230 --> 00:18:43.680 And we talked a little bit about variables, and 338 00:18:43.680 --> 00:18:47.440 of course we mentioned the different types of variables that are built into Python. 339 00:18:47.440 --> 00:18:50.380 Went through discussing number data types, heaps and floats etc., and 340 00:18:50.380 --> 00:18:52.290 then talked about strings. 341 00:18:52.290 --> 00:18:55.940 And we've now seen some basic operations that can be performed on numbers and 342 00:18:55.940 --> 00:18:56.630 strings. 343 00:18:56.630 --> 00:18:58.710 We even talked about operator precedence. 344 00:18:58.710 --> 00:19:02.810 And finally we finished off by slicing using slicing to extract sub-strings, 345 00:19:02.810 --> 00:19:04.410 put string operators on. 346 00:19:04.410 --> 00:19:08.250 And of course, we covered in this video how to actually format strings and 347 00:19:08.250 --> 00:19:09.820 how to print strings and numbers together. 348 00:19:09.820 --> 00:19:11.160 So in the next video what we're gonna do is, 349 00:19:11.160 --> 00:19:14.160 we're gonna start looking at statements that allow us to control the flow 350 00:19:14.160 --> 00:19:18.160 of a program, and also to repeat paths for a program many times. 351 00:19:18.160 --> 00:19:20.480 So, thanks for watching and I'll see you in the next section.