1
00:00:05,400 --> 00:00:09,730
In this video, we're gonna start talking
about string formating in Python.

2
00:00:09,730 --> 00:00:13,210
Now when printing strings and numbers,
it's often gonna be convenient to display

3
00:00:13,210 --> 00:00:18,470
both strings and numbers at the same time,
in a single call to print.

4
00:00:18,470 --> 00:00:21,250
So, for example you might want to
print a description of what the value

5
00:00:21,250 --> 00:00:23,050
is before the value itself.

6
00:00:23,050 --> 00:00:27,050
As you saw, in a previous video,
numbers cannot be canonated or

7
00:00:27,050 --> 00:00:30,310
appended with strings using
the plus operator, because

8
00:00:30,310 --> 00:00:33,800
the presence of that number instructs
Python to actually to attempt addition.

9
00:00:33,800 --> 00:00:37,600
And we saw that actually crashes Python,
because it can't figure out what to do.

10
00:00:37,600 --> 00:00:40,560
So there are a couple of,
different approaches to doing this.

11
00:00:40,560 --> 00:00:43,770
So in this video I'm going to go through
and actually show you some of those.

12
00:00:43,770 --> 00:00:47,720
So as you can see on the screen,
I have created a new project.

13
00:00:47,720 --> 00:00:50,920
A Python project in IntelliJ
called string formating.

14
00:00:50,920 --> 00:00:55,940
I also created a Python file using
the new command in IntelliJ so

15
00:00:55,940 --> 00:00:58,490
that we can actually
start typing our code in.

16
00:00:58,490 --> 00:01:01,330
So lets look at one way to
actually do what I suggested,

17
00:01:01,330 --> 00:01:06,260
which is to actually display a string and
a number in the one print statement.

18
00:01:06,260 --> 00:01:10,030
We're gonna use something
called the string function,

19
00:01:10,030 --> 00:01:11,580
or str as it's correctly known.

20
00:01:11,580 --> 00:01:13,400
So let's use that now.

21
00:01:13,400 --> 00:01:15,210
Gonna type age = 24.

22
00:01:15,210 --> 00:01:19,900
And I'm gonna do printMy age

23
00:01:19,900 --> 00:01:24,180
is +, now I'm gonna do it the old way we

24
00:01:24,180 --> 00:01:27,130
established in a previous video
didn't work just to recap.

25
00:01:27,130 --> 00:01:29,990
So that method will actually
result in Python crashing.

26
00:01:29,990 --> 00:01:31,900
So you can see on the right
hand side there, and

27
00:01:31,900 --> 00:01:36,690
the reason it's crashing is it's actually
trying to concatenate or append a number.

28
00:01:36,690 --> 00:01:39,330
We know that age is a number
because of this line here and

29
00:01:39,330 --> 00:01:43,060
line two to a string, which of course
is something in double quotes.

30
00:01:43,060 --> 00:01:44,380
We can use a method,

31
00:01:44,380 --> 00:01:48,370
which is something that's brought
into Python called the STR method.

32
00:01:49,500 --> 00:01:53,640
And type STR and put a bracket
around the age variable, like so.

33
00:01:53,640 --> 00:01:55,370
And effectively what that is doing,

34
00:01:55,370 --> 00:01:59,060
that line there is actually converting
what's in the brackets to a string.

35
00:01:59,060 --> 00:02:02,350
So consequently when we run that,
we no longer get an error and

36
00:02:02,350 --> 00:02:05,610
we actually get the correct result,
my age is 24 years.

37
00:02:05,610 --> 00:02:10,509
So while other languages such as Java will
do this for you automatically, Java would

38
00:02:10,509 --> 00:02:13,820
not have crashed in that scenario I showed
you on the screen where Python did.

39
00:02:13,820 --> 00:02:14,710
In Python,

40
00:02:14,710 --> 00:02:17,860
we actually need to do it specifically and
this is one way of doing it.

41
00:02:17,860 --> 00:02:21,070
Now, you can probably imagine
if you had four, five,

42
00:02:21,070 --> 00:02:25,830
or more numbers to actually print like
this that it could quickly rather tedious

43
00:02:25,830 --> 00:02:27,590
to having to type that every time.

44
00:02:27,590 --> 00:02:31,070
I'm trying to coerce every
variable using the STR method.

45
00:02:31,070 --> 00:02:34,830
And fortunately for us, Python actually
provides a much better mechanism.

46
00:02:34,830 --> 00:02:38,370
Now what I'm gonna do is, I'm gonna show
you something in Python called replacement

47
00:02:38,370 --> 00:02:41,600
fields and this is gonna be a way that
makes it a lot easier to make changes like

48
00:02:41,600 --> 00:02:44,850
this, when your dealing with
multiple numbers on the one line.

49
00:02:44,850 --> 00:02:47,060
We're gonna do something
called replacement fields.

50
00:02:47,060 --> 00:02:50,200
And later on I'm gonna start
talking about an old method

51
00:02:50,200 --> 00:02:54,160
which was called a formating operator,
and we'll understand why shortly.

52
00:02:54,160 --> 00:02:57,170
But for now, let's just get stuck
into showing you how to do this with

53
00:02:57,170 --> 00:02:58,380
a replacement field.

54
00:02:58,380 --> 00:03:01,640
And probably the best way to actually
explain it is actually to show

55
00:03:01,640 --> 00:03:02,720
you an example of it.

56
00:03:02,720 --> 00:03:06,260
So in the scenario of printing
as we did above we could do

57
00:03:06,260 --> 00:03:09,510
what is equivalent using
these string str method.

58
00:03:09,510 --> 00:03:14,540
We can type, My age is, and

59
00:03:14,540 --> 00:03:19,700
notice I put curly brackets and a number,
the number zero in between them.

60
00:03:19,700 --> 00:03:24,910
And I've typed years dot,
so type .format(age).

61
00:03:24,910 --> 00:03:29,120
So what I've done there,
this is called a replacement field and

62
00:03:29,120 --> 00:03:31,740
this is the actual replacement for
this part here.

63
00:03:31,740 --> 00:03:36,620
So it's the syntax of it is
a curly brace at the start,

64
00:03:36,620 --> 00:03:39,720
a number in the middle and
another curly brace to actually end it.

65
00:03:39,720 --> 00:03:41,800
And that number,
which we'll talk about more shortly,

66
00:03:41,800 --> 00:03:44,780
comes into play when we're actually
dealing with multiple parameters.

67
00:03:44,780 --> 00:03:46,140
But we'll talk more
about that in a minute.

68
00:03:46,140 --> 00:03:47,620
But now let's just run this example,

69
00:03:47,620 --> 00:03:50,030
and we should get exactly
the same result as we got before.

70
00:03:50,030 --> 00:03:53,630
Now it doesn't look to be a particularly
better way of doing things at the moment,

71
00:03:53,630 --> 00:03:56,120
but where it really
starts coming in handy,

72
00:03:56,120 --> 00:03:58,620
is if we've actually got a lot of data and
a lot of parameters.

73
00:03:58,620 --> 00:04:02,160
So what i'm gonna do is I'm gonna
paste in a bit of code here and

74
00:04:02,160 --> 00:04:04,610
I'm going to just actually make it so
it goes up to two lines so

75
00:04:04,610 --> 00:04:05,930
we can actually see it a bit better.

76
00:04:08,040 --> 00:04:12,860
Like so,
I'll just move this over a little bit.

77
00:04:12,860 --> 00:04:15,820
So what we've got is, now we've got
one big string as you can see there,

78
00:04:15,820 --> 00:04:17,630
that's just all one big string.

79
00:04:17,630 --> 00:04:20,040
But notice how we've got
multiple replacement fields now.

80
00:04:20,040 --> 00:04:23,750
So we've got the zero, the one, the,
two, the three, the four, the five,

81
00:04:23,750 --> 00:04:25,120
the six, and the seven.

82
00:04:25,120 --> 00:04:28,160
And in the past, in the previous
example we had doc format and

83
00:04:28,160 --> 00:04:31,640
we only had just the one thing, the one
bit of information which was age, and

84
00:04:31,640 --> 00:04:35,580
that went automatically into this position
here, into have a replacement for zero.

85
00:04:35,580 --> 00:04:39,400
Now, we've actually got a total, if you
count them, one, two, three, four, five,

86
00:04:39,400 --> 00:04:40,800
six, seven, eight.

87
00:04:40,800 --> 00:04:44,820
And they're actually numbered from
zero to seven within the string.

88
00:04:44,820 --> 00:04:46,210
So, what's gonna happen here is,

89
00:04:46,210 --> 00:04:50,500
each one of these is going to be matched
up into the particular placement fields.

90
00:04:50,500 --> 00:04:52,780
So, 31 should go into where it's got zero.

91
00:04:52,780 --> 00:04:56,800
The replacement through zero generate
into number one, march into number two,

92
00:04:56,800 --> 00:04:57,440
and so on.

93
00:04:57,440 --> 00:05:01,500
And when we actually run this,
we should actually see that.

94
00:05:01,500 --> 00:05:03,920
We're gonna open this out so
we can see the result.

95
00:05:03,920 --> 00:05:08,000
There are 31 days in January, March,
May, July, August, October, December.

96
00:05:08,000 --> 00:05:13,240
So I can see what happened, it actually
took those values It took the values in

97
00:05:13,240 --> 00:05:17,610
the .format part of this line and actually
put them automatically in the right spot.

98
00:05:17,610 --> 00:05:21,840
So that's the concept of a replacement
field as you can see, that it does

99
00:05:21,840 --> 00:05:25,330
really make it a lot easier from the point
of view of putting your data together,

100
00:05:25,330 --> 00:05:28,720
cuz we're putting all of the data that
we want outputted in the one place.

101
00:05:28,720 --> 00:05:32,000
We're not having to put a bid here
like putting your plus str and

102
00:05:32,000 --> 00:05:34,870
whatever the thing is that you want
to actually add to that spot as well.

103
00:05:34,870 --> 00:05:37,650
So it's a lot easier to
actually process and work with.

104
00:05:37,650 --> 00:05:40,940
That's the one, the replacement fields
is the new way of doing things.

105
00:05:40,940 --> 00:05:44,000
And with that said,
we can actually make this better as well,

106
00:05:44,000 --> 00:05:46,220
and there's nothing to stop
us from using triple quotes.

107
00:05:46,220 --> 00:05:48,220
So for example, I can come back here and

108
00:05:48,220 --> 00:05:52,010
I can paste in some more code and notice
I'm using the triple quotes again so

109
00:05:52,010 --> 00:05:54,500
that we're actually dealing
with data on multiple lines.

110
00:05:54,500 --> 00:05:59,100
And notice also, I'm reusing
a replacement field more than once.

111
00:05:59,100 --> 00:06:02,940
So number two you can see is used
multiple times as is number one.

112
00:06:02,940 --> 00:06:06,310
And number zero is also used once as well.

113
00:06:06,310 --> 00:06:10,900
So now when we run that example you
can see we've got some data there.

114
00:06:10,900 --> 00:06:11,460
What I might do is,

115
00:06:11,460 --> 00:06:14,220
I might leave this window and then run one
window down to the bottom of the screen so

116
00:06:14,220 --> 00:06:15,470
that we can see things
a little bit better.

117
00:06:16,740 --> 00:06:17,350
Move to bottom.

118
00:06:17,350 --> 00:06:20,010
It'd be a little bit easier for
us to actually read.

119
00:06:20,010 --> 00:06:23,640
So, I'm just going to leave
a little bit of space there.

120
00:06:23,640 --> 00:06:26,740
So you can see that it handles quite
nicely using the triple quotes

121
00:06:26,740 --> 00:06:31,410
to deal with strings of handling
that go over to more than line,

122
00:06:31,410 --> 00:06:33,220
multi-lines in other words.

123
00:06:33,220 --> 00:06:36,790
And obviously compared to the approach
at the top of the line three.

124
00:06:36,790 --> 00:06:39,930
That would be a huge long line to actually
have to type in if you were doing

125
00:06:39,930 --> 00:06:42,200
like that and it would become quite messy.

126
00:06:42,200 --> 00:06:45,560
So it's a great way to actually replace
things and do things in a better way.

127
00:06:45,560 --> 00:06:48,030
And as I said that's being
introduced into Python 3.

128
00:06:48,030 --> 00:06:50,530
Now there is another
way of doing things and

129
00:06:50,530 --> 00:06:52,640
this is called the string
formatting operator.

130
00:06:52,640 --> 00:06:56,300
I mentioned that briefly before and
I'm gonna show you how to use it,

131
00:06:56,300 --> 00:07:00,670
despite the fact that it's now
being deprecated in Python 3.

132
00:07:00,670 --> 00:07:01,760
And when I say deprecate,

133
00:07:01,760 --> 00:07:06,390
what that actually means is that's a
feature that's a part of the program that

134
00:07:06,390 --> 00:07:11,500
actually worked which is now no longer
recommended to be used in Python 3.

135
00:07:11,500 --> 00:07:15,570
So it's introduce into Python 2, and
it's used extensively in Python 2 code.

136
00:07:15,570 --> 00:07:19,920
And actually that's the reason why I'm
going to actually teach you some of this,

137
00:07:19,920 --> 00:07:23,910
because the chances are gonna be high
as you move forward learning Python,

138
00:07:23,910 --> 00:07:27,740
that you'll be getting
access to Python 2 code.

139
00:07:27,740 --> 00:07:31,960
Because Python 2 has been out there for so
long, there's just a huge amount of code

140
00:07:31,960 --> 00:07:35,440
out there and it's kinda pretty important
for you to actually see that and

141
00:07:35,440 --> 00:07:38,240
understand how that works because
it's a fundamental pile for so

142
00:07:38,240 --> 00:07:39,960
many Python 2 programs.

143
00:07:39,960 --> 00:07:43,020
The other thing you'll be able
to do is once you see the way

144
00:07:43,020 --> 00:07:46,170
that this formatting operator works.

145
00:07:46,170 --> 00:07:46,980
If you decide to,

146
00:07:46,980 --> 00:07:51,280
you'll be able to actually convert that
to the new replacement field syntax.

147
00:07:51,280 --> 00:07:54,710
So let's just start from the basics,
and have a look how it works.

148
00:07:54,710 --> 00:08:00,250
So what I'm going to do is start by
typing a formatting operator for

149
00:08:00,250 --> 00:08:01,690
our first example, which was the age.

150
00:08:01,690 --> 00:08:08,060
So print( My age is, and this time
we're doing it a little bit different,

151
00:08:08,060 --> 00:08:13,970
we put %D years, And a percent sign
again and then whatever we want printed,

152
00:08:13,970 --> 00:08:16,950
in this case, it would be the age
variable that we defined in line two.

153
00:08:16,950 --> 00:08:20,210
Now that actually should
have been a lowercase d.

154
00:08:20,210 --> 00:08:22,360
Like so,
it does actually make a difference.

155
00:08:22,360 --> 00:08:26,180
So now if I run that we should find below
all those dates we should get the same

156
00:08:26,180 --> 00:08:29,110
answer that we got when we printed
the example from line two.

157
00:08:29,110 --> 00:08:33,090
And you can see that it still does
come up and says My age is 24 years.

158
00:08:33,090 --> 00:08:34,950
This again is the old format.

159
00:08:34,950 --> 00:08:38,380
So what actually happened
here is you can see

160
00:08:38,380 --> 00:08:41,150
the %d actually got
replaced with an integer.

161
00:08:41,150 --> 00:08:44,800
In the integer of course was a value
that was in the age variable.

162
00:08:44,800 --> 00:08:50,520
So taking it to the next level, we could
actually do something like print, My age

163
00:08:50,520 --> 00:08:54,850
is %d, I can also put %s.

164
00:08:54,850 --> 00:09:01,750
%d, %s let me put a percent again.

165
00:09:01,750 --> 00:09:05,070
This time we put because we've got
multiple things that we wanna print.

166
00:09:05,070 --> 00:09:09,800
We can print those in brackets,
age, years,

167
00:09:09,800 --> 00:09:15,310
six, months.

168
00:09:15,310 --> 00:09:20,400
All right so, now if we actually run
that you can see that it comes up and

169
00:09:20,400 --> 00:09:23,770
says, my age is 24 years, six months.

170
00:09:23,770 --> 00:09:27,340
And you can see what happens is, the first
time it's come along, it's what my age is.

171
00:09:27,340 --> 00:09:30,790
It's looked at the variable age and
so that needs to be 24 or

172
00:09:30,790 --> 00:09:33,000
in this case I just put a 24 in there.

173
00:09:33,000 --> 00:09:36,810
The next one was a %s,
s stands for string.

174
00:09:36,810 --> 00:09:40,310
And so therefore let's put in the value,
in this case the years,

175
00:09:40,310 --> 00:09:42,770
which was a literal
string that we typed in.

176
00:09:42,770 --> 00:09:47,100
Put it in that spot and you can see that
it's done the same thing for the six and

177
00:09:47,100 --> 00:09:48,350
the months as well.

178
00:09:48,350 --> 00:09:52,210
So you can actually have
multiple replacements going on

179
00:09:52,210 --> 00:09:53,970
in the one print line,
as you can see there.

180
00:09:53,970 --> 00:09:58,160
So replacement's strictly one value at
a time, from left to right, and it's got

181
00:09:58,160 --> 00:10:02,810
pretty well none of the flexibility
offered by Python 3 replacement fields.

182
00:10:02,810 --> 00:10:06,450
So you should really be focusing on that,
replacement fields where possible.

183
00:10:06,450 --> 00:10:09,030
But again, I'm showing you this
now just so you can look at

184
00:10:09,030 --> 00:10:12,930
type Python 2 code at some point if you
need to, and understand how it works.

185
00:10:12,930 --> 00:10:16,200
Now just to take that a little bit
further, let's look at another example and

186
00:10:16,200 --> 00:10:17,110
that's using the for loop.

187
00:10:17,110 --> 00:10:19,640
And we're going to use the for
loop one more time, bearing

188
00:10:19,640 --> 00:10:23,450
in mind that we're going to cover the for
loop officially in the next section.

189
00:10:23,450 --> 00:10:26,140
But for now,
let's just focus on the print statement.

190
00:10:26,140 --> 00:10:31,010
So I'm going to type for, i, in range,

191
00:10:31,010 --> 00:10:36,900
one comma 12, colon, print, number %2d.

192
00:10:36,900 --> 00:10:43,390
And notice I put a number there this time,

193
00:10:43,390 --> 00:10:49,140
squared is %4d and cubed is %4d.

194
00:10:51,560 --> 00:10:56,430
Then for the answers I'm gonna put %i,
i, star, star, two, and

195
00:10:56,430 --> 00:11:02,080
it sort of multiply in the previous video
the two stars together that indicates

196
00:11:02,080 --> 00:11:08,300
that we're actually doing, we're trying to
raise a number to the power of another.

197
00:11:08,300 --> 00:11:12,890
So i ** 2 in this case so
i ** 2 is i squared.

198
00:11:12,890 --> 00:11:15,670
And the next one, I'm gonna do i **, or

199
00:11:15,670 --> 00:11:20,970
to the power of, three like so,
gives us an answer that's i cubed.

200
00:11:20,970 --> 00:11:24,310
Now if we actually run this and
actually have a look at the result

201
00:11:24,310 --> 00:11:28,000
you can see on the output,
we've got things nicely formatted.

202
00:11:28,000 --> 00:11:31,170
So the for loop's gone through and
done the processing for everything, and

203
00:11:31,170 --> 00:11:34,630
in terms of printing out the numbers,
you can see how there's been

204
00:11:34,630 --> 00:11:37,200
a certain amount of width that's
been allocated for each section.

205
00:11:37,200 --> 00:11:40,110
So even though it didn't
need to use the letter, or

206
00:11:40,110 --> 00:11:44,590
the space before the number 1 We actually
put the number in there, we said two,

207
00:11:44,590 --> 00:11:49,110
use up to two, or to allocate two
spaces for this var actually working.

208
00:11:49,110 --> 00:11:53,380
Consequently, it allocated that space and
just made it blank if it wasn't in use.

209
00:11:53,380 --> 00:11:57,500
And as a result you can see that things
are nicely formatted and nicely in order.

210
00:11:57,500 --> 00:12:01,240
And just to confirm that,
if we actually remove that and

211
00:12:01,240 --> 00:12:02,620
run this without those numbers in there.

212
00:12:02,620 --> 00:12:06,060
It does still work but
it doesn't look anywhere near as neat.

213
00:12:06,060 --> 00:12:09,770
Yes, the first part does up until we've
got a number that's greater than nine.

214
00:12:09,770 --> 00:12:13,280
Then you see it starts really losing
the actually formatting quite badly.

215
00:12:13,280 --> 00:12:15,570
So I'm gonna put that back
to how it was before again.

216
00:12:16,820 --> 00:12:20,080
Run it again so we've got some
nice formatting going on and

217
00:12:20,080 --> 00:12:23,150
there's other types of operators
you can actually do there.

218
00:12:23,150 --> 00:12:26,310
We've actually put %d,
is the one that's for

219
00:12:26,310 --> 00:12:30,670
an integer and you saw %s before,
s plus four a string.

220
00:12:30,670 --> 00:12:34,710
Now another thing you can actually do is
you can actually specify the precision of

221
00:12:34,710 --> 00:12:35,710
a number as well.

222
00:12:35,710 --> 00:12:36,890
So let's go ahead and actually do that.

223
00:12:36,890 --> 00:12:42,080
And the way to do that, and
we can talk for example print pi is

224
00:12:42,080 --> 00:12:49,840
approximately %12f.

225
00:12:49,840 --> 00:12:54,740
And if type 22 divided by 7,
if you run that, you can

226
00:12:58,750 --> 00:13:02,780
see by default we've got 3.142857.

227
00:13:02,780 --> 00:13:05,180
And the calculation's actually
six decimal places wide.

228
00:13:05,180 --> 00:13:06,480
But we can actually request more.

229
00:13:06,480 --> 00:13:08,890
So we could change the format stream,
which we'll do now.

230
00:13:08,890 --> 00:13:10,020
Let's change that, say, to 50.

231
00:13:10,020 --> 00:13:16,350
So we'll come here where it's got 12f and
we'll change that to 12.50.

232
00:13:16,350 --> 00:13:19,030
So we're saying that we want
a lot more precision there.

233
00:13:19,030 --> 00:13:20,340
So if we do that and run that again.

234
00:13:21,450 --> 00:13:24,890
You can see, we've now got a number
that literally has 50 decimal points.

235
00:13:24,890 --> 00:13:28,460
So it's far more detailed, far more
precise than what the previous one was.

236
00:13:28,460 --> 00:13:31,580
And it's just adding these numbers
in the various part of this,

237
00:13:31,580 --> 00:13:35,550
after the percent sign and before
the actual type, which signifies and

238
00:13:35,550 --> 00:13:37,650
actually shows where that
position is gonna be.

239
00:13:37,650 --> 00:13:39,270
Whether it's gonna be
after the decimal point or

240
00:13:39,270 --> 00:13:42,660
whether it's sort of a gap, in the number
to make sure that it lines up nicely.

241
00:13:42,660 --> 00:13:47,500
So once again, the thing to keep in mind
is that you probably wont be using these

242
00:13:47,500 --> 00:13:50,390
and you shouldn't be using
those in Python 3 generally.

243
00:13:50,390 --> 00:13:52,790
And this is the string formating
operators I'm talking about,

244
00:13:52,790 --> 00:13:54,430
these with the percent signs.

245
00:13:54,430 --> 00:13:57,360
But it is important to understand them
because you almost certainly will

246
00:13:57,360 --> 00:13:59,680
come across them in your Python codes.

247
00:13:59,680 --> 00:14:03,580
And what I've in the screen now, I'm
gonna just open a browser and show you,

248
00:14:03,580 --> 00:14:07,840
this page here and there's a link to
this page in the actual video itself.

249
00:14:07,840 --> 00:14:11,370
So go to the resources section of this
video and you may have to come here and

250
00:14:11,370 --> 00:14:14,250
actually find out more about
string formatting operators and

251
00:14:14,250 --> 00:14:17,900
how they apply to the older version or
Python version 2.

252
00:14:17,900 --> 00:14:22,830
By getting back to replacement fields,
what we've done here we can

253
00:14:22,830 --> 00:14:26,700
pretty well do it identically and
more with replacement fields.

254
00:14:26,700 --> 00:14:28,660
So let's try another example.

255
00:14:28,660 --> 00:14:30,210
We're gonna copy the first part.

256
00:14:32,600 --> 00:14:36,290
Like so, and then we're gonna actually
change it into, placed with some syntax.

257
00:14:36,290 --> 00:14:39,010
So I'm gonna type zero
which we did before, but

258
00:14:39,010 --> 00:14:40,970
I'm gonna but a colon and a number.

259
00:14:40,970 --> 00:14:42,970
That's gonna be how wide.

260
00:14:42,970 --> 00:14:45,670
We're actually allocating space four.

261
00:14:45,670 --> 00:14:51,000
And likewise I can do 1:4 and

262
00:14:51,000 --> 00:14:54,080
cubed, 2:4.

263
00:14:54,080 --> 00:14:59,310
And instead of the percent which was
of course the old way of doing things,

264
00:14:59,310 --> 00:15:01,920
we do type.format as we did earlier and

265
00:15:01,920 --> 00:15:04,610
then we just have the same
parameters as they had before.

266
00:15:04,610 --> 00:15:07,900
So when we run that we should
get an identical result but

267
00:15:07,900 --> 00:15:10,450
using the new replacement field syntax.

268
00:15:10,450 --> 00:15:13,320
You can see that we have
everything nicely formated.

269
00:15:13,320 --> 00:15:15,640
Much as we had with the old approach but

270
00:15:15,640 --> 00:15:18,240
obviously using the new
replacement field syntax.

271
00:15:18,240 --> 00:15:20,230
Now, that's what's
specifying the width again.

272
00:15:20,230 --> 00:15:23,600
So just to be clear, the first one
was the replacement for your number.

273
00:15:23,600 --> 00:15:26,090
And that's which value from

274
00:15:26,090 --> 00:15:30,050
this area after the .format is
actually used in which spot.

275
00:15:30,050 --> 00:15:32,260
But the other part after that,
of course, was the width.

276
00:15:32,260 --> 00:15:37,110
So colon two meant we allocated a width
of 2:4, we allocated a width of four, and

277
00:15:37,110 --> 00:15:39,240
so on and so forth.

278
00:15:39,240 --> 00:15:41,500
Now we can also tidy them
up a little bit though.

279
00:15:41,500 --> 00:15:45,540
So what we can do is we can ensure that
numbers occupy the same field width but

280
00:15:45,540 --> 00:15:49,750
are left aligned by placing a less
than sign after the symbol.

281
00:15:49,750 --> 00:15:53,860
So what we can do there is, come down
here, we can leave the first one as it is,

282
00:15:53,860 --> 00:15:58,850
but the second one, with the squared
result, we'll put less than four,

283
00:15:58,850 --> 00:16:01,650
down here to two we put
less than four there.

284
00:16:01,650 --> 00:16:06,420
If we actually run that,
notice how things are now left formatted.

285
00:16:06,420 --> 00:16:08,510
We're still actually allocating
the same amount of space.

286
00:16:08,510 --> 00:16:12,450
So things are still lined up but
effectively we're left justifying

287
00:16:12,450 --> 00:16:15,330
the numbers so the number will always
start from the left hand side.

288
00:16:15,330 --> 00:16:17,330
That's just another way
of doing things as well.

289
00:16:17,330 --> 00:16:21,650
And what we could do for
the precision example coming back here.

290
00:16:21,650 --> 00:16:23,820
We could actually just
copy the Pi result again.

291
00:16:23,820 --> 00:16:28,860
So I'll copy that line,
put it down here and we could change that.

292
00:16:28,860 --> 00:16:33,380
Where we had %12f, we could actually
delete that change it to replacement

293
00:16:33,380 --> 00:16:38,570
fields format 0:12 to
add the precision .50.

294
00:16:38,570 --> 00:16:42,210
So you can see that what you could do
with the old format in the new format

295
00:16:42,210 --> 00:16:45,480
using replacement fields quite easily,
and change that of course to format.

296
00:16:45,480 --> 00:16:46,840
And there's only the one parameter,

297
00:16:46,840 --> 00:16:50,370
cuz it's only the one replacement
field that we're using.

298
00:16:50,370 --> 00:16:53,840
And if we run that,
we get the same result with pi,

299
00:16:53,840 --> 00:16:55,430
as you can see down on
the bottom of the screen.

300
00:16:57,080 --> 00:17:00,110
So it's quite easy to convert
the old style modifiers,

301
00:17:00,110 --> 00:17:04,290
as you saw me do in this video and replace
and convert those into the new .format or

302
00:17:04,290 --> 00:17:06,060
the new replacement fields syntax.

303
00:17:06,060 --> 00:17:08,619
The other thing I wanted to mention,
was two other things I wanted to mention

304
00:17:08,619 --> 00:17:11,460
before we end this section and
I wanted to paste one example in here.

305
00:17:11,460 --> 00:17:13,800
I'm not sure whether we made
it clear enough here, but

306
00:17:13,800 --> 00:17:15,970
we can actually do something like this.

307
00:17:15,970 --> 00:17:19,900
You see that we've used the actual
replacement numbers out of order for

308
00:17:19,900 --> 00:17:21,050
the replacement fields.

309
00:17:21,050 --> 00:17:24,079
So it doesn't have to start from zero,
one,

310
00:17:24,079 --> 00:17:27,810
two and unlike the actual percentage
approach with these modifiers,

311
00:17:27,810 --> 00:17:30,230
we can only use those once,
and they have to be in order.

312
00:17:30,230 --> 00:17:32,470
In this case we can reuse
them multiple times.

313
00:17:32,470 --> 00:17:36,120
So you can see here that we're actually
showing we're using a replacement field,

314
00:17:36,120 --> 00:17:38,540
like number two was used
in multiple positions.

315
00:17:38,540 --> 00:17:40,690
That's one of the other reasons
to use replacement fields.

316
00:17:40,690 --> 00:17:44,530
And of course if you run it,
we'll see that it works quite nicely.

317
00:17:44,530 --> 00:17:45,500
Now just in finishing off,

318
00:17:45,500 --> 00:17:49,400
the other thing to note that in terms
of those numbers, keep in mind that

319
00:17:49,400 --> 00:17:52,280
actually the use of numbers in
replacement fields are actually optional.

320
00:17:52,280 --> 00:17:57,450
So we can actually do something
like this if we prefer And

321
00:17:57,450 --> 00:17:59,980
you can see there I'm going
through the same range again, but

322
00:17:59,980 --> 00:18:03,770
notice how I'm not actually inferring, or
not actually typing in a number at all.

323
00:18:03,770 --> 00:18:07,210
So what will happen there is that will
automatically assume that the first one

324
00:18:07,210 --> 00:18:10,720
you use will be zero, the second one will
be one, and the third one will be two.

325
00:18:10,720 --> 00:18:12,290
So if we run that we'll see that.

326
00:18:13,520 --> 00:18:15,720
And you can see we still
got the correct results.

327
00:18:16,990 --> 00:18:19,560
And note how the third one,
even though we didn't specify a number,

328
00:18:19,560 --> 00:18:21,170
we can still actually control the layout.

329
00:18:21,170 --> 00:18:25,610
But of course in that scenario, what you
couldn't do is use the value more than

330
00:18:25,610 --> 00:18:29,100
once, as we used in the example above,
because of course we need to

331
00:18:29,100 --> 00:18:32,590
actually specify that number if we
aregonna use it more than once.

332
00:18:32,590 --> 00:18:35,540
But in those situations where you don't
need to actually use something more than

333
00:18:35,540 --> 00:18:37,630
once, this is probably gonna be ideal for
you, and

334
00:18:37,630 --> 00:18:39,640
it's a little bit less to type as well.

335
00:18:39,640 --> 00:18:41,990
Okay, so that's it, that's actually
the end of this section, so

336
00:18:41,990 --> 00:18:44,640
we've actually looked at running
a simple hello world Python program.

337
00:18:44,640 --> 00:18:47,090
And we talked a little
bit about variables, and

338
00:18:47,090 --> 00:18:50,850
of course we mentioned the different types
of variables that are built into Python.

339
00:18:50,850 --> 00:18:53,790
Went through discussing number data types,
heaps and floats etc., and

340
00:18:53,790 --> 00:18:55,700
then talked about strings.

341
00:18:55,700 --> 00:18:59,350
And we've now seen some basic operations
that can be performed on numbers and

342
00:18:59,350 --> 00:19:00,040
strings.

343
00:19:00,040 --> 00:19:02,120
We even talked about operator precedence.

344
00:19:02,120 --> 00:19:06,220
And finally we finished off by slicing
using slicing to extract sub-strings,

345
00:19:06,220 --> 00:19:07,820
put string operators on.

346
00:19:07,820 --> 00:19:11,660
And of course, we covered in this video
how to actually format strings and

347
00:19:11,660 --> 00:19:13,230
how to print strings and numbers together.

348
00:19:13,230 --> 00:19:14,570
So in the next video
what we're gonna do is,

349
00:19:14,570 --> 00:19:17,570
we're gonna start looking at statements
that allow us to control the flow

350
00:19:17,570 --> 00:19:21,570
of a program, and also to repeat paths for
a program many times.

351
00:19:21,570 --> 00:19:23,890
So, thanks for watching and
I'll see you in the next section.

