1
00:00:04,190 --> 00:00:09,660
So string replacement fields can also
contain formatting, to display values

2
00:00:09,660 --> 00:00:13,200
with a specific number of decimal places,
for example.

3
00:00:13,200 --> 00:00:17,100
So let's create a new Python file. I'm going to call it formatting,

4
00:00:23,020 --> 00:00:27,140
and I'm going to use a for loop to
generate some output.

5
00:00:27,140 --> 00:00:30,980
We'll be looking at for loops in the next section, so don't worry about what's happening.

6
00:00:30,980 --> 00:00:34,040
Concentrate on the print lines that I'm going to type in this code, to see

7
00:00:34,040 --> 00:00:37,940
the effect of the formatting in the
replacement fields. So we're going to

8
00:00:37,940 --> 00:00:43,200
start by printing the values from 1 to 12, with the values of their squares and cubes.

9
00:00:43,200 --> 00:00:50,560
So on line 1 I'm going to type
for i in range(1, 13):

10
00:00:50,560 --> 00:00:55,079
and put a colon to the right of the
right parenthesis. So on line 2 I'm

11
00:00:55,079 --> 00:00:59,850
going to press ENTER here. You need to
make sure there's 4 spaces before print,

12
00:00:59,850 --> 00:01:05,420
which I'm about to type, on line 2. I'm
going to type print parentheses,

13
00:01:05,420 --> 00:01:09,780
and in double quotes I'm going to type number, or the abbreviation No, period. Then I'm

14
00:01:09,780 --> 00:01:13,200
going to put replacement field 0, which
is our left and right curly braces with a

15
00:01:13,200 --> 00:01:25,920
0 in the middle, squared is, and replacement field 1, and cubed is, replacement field 2.

16
00:01:25,920 --> 00:01:31,680
Then I'm going to type dot
format parentheses i, i. I'm going to use

17
00:01:31,680 --> 00:01:37,920
the exponent operator, which is 2
multiplication signs, 2 then i, exponent

18
00:01:37,920 --> 00:01:42,030
operator again, 3, which is to the
power. It's basically raising one number

19
00:01:42,030 --> 00:01:44,340
to the power of another in Python.

20
00:01:44,340 --> 00:01:50,960
Alright so if we run this now,

21
00:01:50,960 --> 00:01:55,140
and you can see we've got the values of the
number, the value squared, and the value

22
00:01:55,140 --> 00:01:59,520
cubed, for each of those numbers. And in
case it's not obvious now, you can

23
00:01:59,520 --> 00:02:04,200
provide any expressions inside the
format parentheses. They don't have to be

24
00:02:04,200 --> 00:02:09,720
variables or literal numbers. On line 2,
we've got i exponent operator 2 to get

25
00:02:09,720 --> 00:02:14,520
the number squared, and i exponent
operator 3 to get the number cubed.

26
00:02:14,520 --> 00:02:17,700
And the exponent operator, is how you can raise one

27
00:02:17,700 --> 00:02:22,020
number to the power of another in Python. So that's the power to operator as well.

28
00:02:22,020 --> 00:02:25,700
Alright so that works, but as
you can see, the numbers aren't - or the

29
00:02:25,709 --> 00:02:29,790
values rather - aren't lined up. So we can
fix that in the output, or we'll fix the

30
00:02:29,790 --> 00:02:34,050
output by applying some formatting. So
firstly, I'm going to specify a field width

31
00:02:34,050 --> 00:02:38,310
for each of the replacement fields. So
all the values for i are a maximum of

32
00:02:38,310 --> 00:02:42,120
two digits, because we're, obviously, only counting
from 1 to 12 here. So we can use a

33
00:02:42,120 --> 00:02:44,700
field width of 2 for the first
replacement field.

34
00:02:44,700 --> 00:02:48,930
I'm going to use a field width of 4
for the other two fields. So to actually

35
00:02:48,930 --> 00:02:53,910
specify width, we come back up to our
line 2, and we add the width by putting a

36
00:02:53,910 --> 00:02:57,090
colon and then the number that we need.
So for the first replacement field

37
00:02:57,090 --> 00:03:01,860
zero,  I'm going to put :2 there, for a
width of 2, and for the other two

38
00:03:01,860 --> 00:03:13,010
replacement fields I'm going to put :4 and :4 again. So if we run the program now,

39
00:03:14,080 --> 00:03:18,970
it's now easier to understand, because the
formatting makes it easier to see at a

40
00:03:18,970 --> 00:03:22,480
glance, what the values are. Alright so that
looks tidier. The values are now

41
00:03:22,480 --> 00:03:27,610
lining up nicely.
So on line 2, we're using 0:2 in

42
00:03:27,610 --> 00:03:30,420
our left and right curly braces for the
first replacement field.

43
00:03:30,420 --> 00:03:35,780
2 is the field width, as I mentioned,
and it's separated from the index with a colon.

44
00:03:35,780 --> 00:03:38,920
Now everything in that first
column prints in a width of two

45
00:03:38,920 --> 00:03:43,040
characters. So think of it as
reserving two spaces on the screen,

46
00:03:43,040 --> 00:03:46,960
so that the one digit values still line up
with the two digit ones, and you can see

47
00:03:46,960 --> 00:03:50,710
they're happening there in the output.
Now our squares also line up, in a field

48
00:03:50,710 --> 00:03:54,490
width of four characters. Maybe a width
of three would look better there; have

49
00:03:54,490 --> 00:03:57,370
a got at changing that yourself, and see
what it looks like. Pause the video

50
00:03:57,370 --> 00:04:01,260
and I'll make the change when you come back.

51
00:04:01,490 --> 00:04:05,480
Okay, so if we only want to use three
spaces for the square values, the change is

52
00:04:05,480 --> 00:04:09,530
obviously easy enough. We come back up to
replacement field 1, change the 4 to

53
00:04:09,530 --> 00:04:15,950
a 3, and if we run it again,
well that looks tidier still now, as

54
00:04:15,950 --> 00:04:20,750
you can see. There's now only one space
between is and the value on the last

55
00:04:20,750 --> 00:04:25,610
three rows. We can also align the values
in their field width. To left align the

56
00:04:25,610 --> 00:04:30,410
values we place a < symbol after
the colon. So what I'm going to do is,

57
00:04:30,410 --> 00:04:36,290
just to make this easier to see, I'm going
to firstly add a blank line. Then I'm

58
00:04:36,290 --> 00:04:40,670
going to take a copy of the line - of the
two lines there. We'll just run it again

59
00:04:40,670 --> 00:04:45,480
so the original code's there as well. So I'm going to paste that in as it was before.

60
00:04:45,480 --> 00:04:49,440
So this time what I'm going to do, again we're
trying to left align the values,

61
00:04:49,440 --> 00:04:53,500
so we'll leave replacement field 0 as it
is, the first one. For the second one

62
00:04:53,510 --> 00:04:55,790
we're going to put, before the three, I'm
going to put the less than sign there,

63
00:04:55,790 --> 00:05:02,620
and for replacement field 2, we're going
to put a less than there as well.

64
00:05:02,620 --> 00:05:06,000
So if we run this,

65
00:05:06,000 --> 00:05:08,960
and the values are now left
aligned, and if it's not immediately

66
00:05:08,960 --> 00:05:12,050
clear to you what's going on there,
scroll up the output to compare the

67
00:05:12,050 --> 00:05:15,830
two blocks. But if we have a look at the
formatting here, notice how it's now left

68
00:05:15,830 --> 00:05:21,360
aligned. If we go back up to the top, you
can see clearly that was right aligned.

69
00:05:21,360 --> 00:05:26,020
So the alignment symbols are quite
visual. The less than, left aligns,

70
00:05:26,020 --> 00:05:30,640
the greater than will right align, and a caret
symbol, well that centers within the

71
00:05:30,640 --> 00:05:34,440
field width. So let's center the last
column to see what that looks like.

72
00:05:34,440 --> 00:05:38,240
So instead of using the less than there, I'm
going to change that by using the caret

73
00:05:38,240 --> 00:05:43,960
symbol instead. And if you run this code
again,

74
00:05:43,960 --> 00:05:48,620
you can see the values are now clearly centered, compared to the
first output,

75
00:05:48,620 --> 00:05:50,900
which was right justified on the right hand side

76
00:05:50,900 --> 00:05:53,900
and this is
obviously for the cubed values.

77
00:05:53,900 --> 00:05:57,650
Now keep in mind that we don't get half
spacing in a terminal, so the result

78
00:05:57,650 --> 00:06:02,800
isn't as accurate as centering values in
a GUI program, but it's available if you want it.

79
00:06:02,800 --> 00:06:06,800
So for floating-point numbers
you can specify a precision - the number

80
00:06:06,800 --> 00:06:11,480
of digits after the decimal point. For
our precision, we specify the precision

81
00:06:11,480 --> 00:06:14,960
after a decimal point, following the width.

82
00:06:14,960 --> 00:06:19,180
So let's have a go at doing that, so I'm going to type some more code in.

83
00:06:19,180 --> 00:06:21,620
So I'm going to put an empty line in there,

84
00:06:21,620 --> 00:06:24,660
and the line after, well
actually line 11, I'm going to type print

85
00:06:24,660 --> 00:06:30,580
parentheses Pi is approximately,

86
00:06:30,580 --> 00:06:34,980
we're going to put replacement field 0:12,

87
00:06:34,980 --> 00:06:41,020
which is the precision for a floating-point number, then dot format

88
00:06:41,020 --> 00:06:47,599
parentheses 22 divided by 7, and 2 right parentheses
to close it off. And let's take a copy of

89
00:06:47,600 --> 00:06:52,680
that line, we're going to make some
modifications. So I need another 5 lines.

90
00:06:52,680 --> 00:06:55,700
I'm going to paste that 5 times.

91
00:06:57,300 --> 00:07:02,860
Alright, so on line 12 what we're going to
do, is we're going to put an f after the 12.

92
00:07:02,860 --> 00:07:17,740
Next line, we going to put 12.50f, line 14
we're going to put 0:52.50f,

93
00:07:17,740 --> 00:07:29,340
and the next, I'm going to put 62.50f, and
the last line, I'm going to put 72.50f.

94
00:07:29,340 --> 00:07:34,640
So the first line of output, well
actually, I'll run it first and then we'll talk about it.

95
00:07:34,640 --> 00:07:38,620
So the first line of output, and that's produced from the code on line 12.

96
00:07:38,620 --> 00:07:42,720
I'm going to see if I can get that on the
screen at the same time, on line 11 rather,

97
00:07:42,720 --> 00:07:47,360
well that's the general format, 
and that defaults to printing 15 decimals.

98
00:07:47,360 --> 00:07:53,020
When we specify a
floating-point value using the f on line 12,

99
00:07:53,020 --> 00:07:57,080
we get the default of 6 digits after
the decimal point.

100
00:07:57,080 --> 00:08:02,320
Now on lines 13 through 16, we're still
specifying a floating-point format,

101
00:08:02,320 --> 00:08:07,620
but we also add a precision of 50 and that
gives 50 points after the decimal point.

102
00:08:07,620 --> 00:08:11,920
The third line of output corresponds to
line 13. If you think it doesn't look

103
00:08:11,930 --> 00:08:15,409
right that's because Python won't
truncate a number. We can't put a value

104
00:08:15,409 --> 00:08:21,319
that's got 50 decimals in a field width
of 12. Python decides that precision is

105
00:08:21,319 --> 00:08:25,069
more important than field width, and
ignores the value 12 that we've

106
00:08:25,069 --> 00:08:29,210
specified for the width, and that's the
code on line 13.

107
00:08:29,210 --> 00:08:32,779
Now the next three lines after that,
print the same value but in different

108
00:08:32,779 --> 00:08:36,589
field widths. When we specify width
greater than 50, you can see that the

109
00:08:36,589 --> 00:08:40,370
effect becomes clear. Now the maximum
precision of a Python floating-point

110
00:08:40,370 --> 00:08:45,260
number is between 51 and 53 digits.
There's not much point specifying a

111
00:08:45,260 --> 00:08:48,890
precision greater than that. Now this
will be easier to see if I left align,

112
00:08:48,890 --> 00:08:52,459
that last line, so I'm going to do that,
and then copy it and increase the

113
00:08:52,459 --> 00:08:59,660
precision just to show you what I mean.
I'm going do that, copy that last line 16.

114
00:08:59,660 --> 00:09:07,100
I'm going to change the 72.50f to 72.54f,
but we're also going to use the

115
00:09:07,100 --> 00:09:10,380
left align, so you can see the value
left aligned, and we'll put that

116
00:09:10,380 --> 00:09:15,780
to the left of the 72 after the colon. We run the
program now,

117
00:09:15,780 --> 00:09:16,640
and what I should do is,

118
00:09:16,640 --> 00:09:19,760
actually, left align the previous line
as well just so you can see the differences.

119
00:09:19,760 --> 00:09:24,880
So we'll do that again as
well on line 16, we'll run that again,

120
00:09:24,880 --> 00:09:28,300
and you can see we only get one extra digit
in the output. We can see that there's

121
00:09:28,300 --> 00:09:31,880
four digits here but the remaining
digits are just padded with zeros,

122
00:09:31,880 --> 00:09:35,740
so there's no actual value there. Alright
so I'll finish this video now by

123
00:09:35,750 --> 00:09:40,040
mentioning that the field number in
replacement fields is optional.

124
00:09:40,040 --> 00:09:44,880
If they're not specified, then Python takes
the value from the format method in order.

125
00:09:44,880 --> 00:09:48,960
So to see what I mean, I'm gonna
take a copy of the code again,

126
00:09:48,960 --> 00:09:54,320
this code up here on line six. I'll take a copy of
that again.

127
00:09:54,320 --> 00:09:57,660
I'm gonna paste it down here on line 19,

128
00:09:57,660 --> 00:10:01,160
and I'm going to actually remove
everything. So we're just going to put

129
00:10:01,160 --> 00:10:06,600
just a left and right curly brace only,
for the three replacement fields,

130
00:10:06,600 --> 00:10:09,320
and actually, what I'll do on the third
replacement field, I'll just put a

131
00:10:09,320 --> 00:10:14,000
colon 4 there, so we can see that we're still
specifying a width.

132
00:10:14,000 --> 00:10:17,060
If we run this,

133
00:10:17,060 --> 00:10:21,620
we can see that the third field shows that you can still use a colon to control the layout,

134
00:10:21,620 --> 00:10:25,240
even if you haven't specified a
field number. All the values in the final

135
00:10:25,250 --> 00:10:29,390
column of output are printing in a field
with the 4. Now if you don't provide

136
00:10:29,390 --> 00:10:34,130
field numbers, you can't use a value more
than once, nor can you change the order

137
00:10:34,130 --> 00:10:38,029
in which values are used. Now our earlier
example, with the number of days in the month,

138
00:10:38,029 --> 00:10:41,260
wouldn't have actually worked without
field numbers.

139
00:10:41,260 --> 00:10:45,040
Alright, so experiment with different
values for the field widths and precision's,

140
00:10:45,040 --> 00:10:47,040
to see how it affects your output,

141
00:10:47,040 --> 00:10:50,820
and I'll see you in the next video.

