1
00:00:04,340 --> 00:00:09,660
Python 2 had another way of formatting
strings, called string interpolation.

2
00:00:51,980 --> 00:00:55,900
Just a reminder here, that if you want to download the slides, you can do so. All the

3
00:00:56,120 --> 00:00:58,120
details are right here in this bonus video.

4
00:00:58,310 --> 00:01:03,300
Right down at the bottom of this course. The very last video in fact, in the course. So just watched that video,

5
00:01:03,500 --> 00:01:04,900
it's got all the details

6
00:01:04,900 --> 00:01:10,140
you need, to be able to download those slides. if you want to do that and be able to access them offline

7
00:01:12,140 --> 00:01:14,240
What I'm going to do is create a new file.

8
00:01:14,240 --> 00:01:21,180
We're going to call that
interpolation.

9
00:01:21,180 --> 00:01:26,320
Now the Python 2 formatting operator is the % and
it's followed in the string by a letter,

10
00:01:26,320 --> 00:01:31,940
to indicate how the formatting should
work. So I'm going to start by typing age = 24 on line 1.

11
00:01:31,940 --> 00:01:36,760
Line 2, I'm going to type print parentheses double quote,
or quotes rather,

12
00:01:36,760 --> 00:01:43,180
My age is, and the percent sign, as I mentioned, d years,

13
00:01:43,180 --> 00:01:53,560
and a closing double quote space, and % age, and closing right parenthesis. So here the %d will

14
00:01:53,560 --> 00:01:57,910
be replaced by an integer value that's
provided after the string, following

15
00:01:57,910 --> 00:02:03,940
another % character. So if we run
that, just to confirm that it works,

16
00:02:03,940 --> 00:02:08,590
and there's the output: My age is 24 years. Now if
you wanted to inject a float into a

17
00:02:08,590 --> 00:02:13,420
string we'd use %f, and then we'll
have to provide a string, and to provide

18
00:02:13,420 --> 00:02:19,990
a string we'd use %s. Let's have
a look at both of those, with some code.

19
00:02:19,990 --> 00:02:29,270
I'm going to start on line 4 by typing major
= "years". Line 5 minor = "months"

20
00:02:29,280 --> 00:02:36,440
and then I continue typing on line 6, print
double quotes My age is %d

21
00:02:36,440 --> 00:02:43,040
space %s comma %d space %s

22
00:02:43,040 --> 00:02:45,420
and then our double quote, and then outside of that

23
00:02:45,420 --> 00:02:53,600
I'm gonna put in parentheses, age, major, 6, minor,

24
00:02:53,600 --> 00:02:57,520
and the two closing right parentheses. And we'll
continue on with an example using PI

25
00:02:57,520 --> 00:03:09,160
again on line 7, print parentheses double
quotes Pi is approximately %f

26
00:03:09,160 --> 00:03:17,620
double quote and the % again, parentheses 22 divided by 7 and two right parentheses.

27
00:03:17,620 --> 00:03:23,840
And I forgot to put the percent sign, of
course, and we need to do that on line 6.

28
00:03:23,840 --> 00:03:29,650
Okay. So starting on line 6, the first
%d is replaced by the first

29
00:03:29,650 --> 00:03:35,520
variable, age, then the first %s is
replaced by the string variable, major.

30
00:03:35,520 --> 00:03:39,920
The second %d is replaced by the
value 6 and the final %s

31
00:03:39,920 --> 00:03:45,360
is replaced by the string variable, minor.
Replacement is strictly one value at a

32
00:03:45,370 --> 00:03:50,040
time, from left to right, with none of the
flexibility offered by Python 3's

33
00:03:50,040 --> 00:03:57,200
replacement fields. So moving on to line
7, we've used %f to represent the floating-point result.

34
00:03:57,200 --> 00:04:02,720
So if we run this now, and you see we get the relevant results.

35
00:04:02,720 --> 00:04:05,820
Alright, so now that we've run
that, you can also specify the precision

36
00:04:05,830 --> 00:04:10,030
of the number. What I'm going to do is
duplicate line 7, then we'll add

37
00:04:10,030 --> 00:04:17,220
add the precision as well. And the way we
do that, instead of just putting %f there,

38
00:04:17,220 --> 00:04:28,380
we change the percent. We type
60.50 to specify the precision and then we run that.

39
00:04:28,380 --> 00:04:30,660
You can see we get the, basically, a similar

40
00:04:30,660 --> 00:04:35,790
result to when we were using a
replacement field. So as well as %d, %s

41
00:04:35,790 --> 00:04:41,550
and %f, we can also use %x to display
numbers in hexadecimal, %o for

42
00:04:41,550 --> 00:04:45,780
octal and %e for scientific
notation - along the lines of one

43
00:04:45,780 --> 00:04:50,550
1.23456e+09, for example. Now there are more modifiers

44
00:04:50,550 --> 00:04:54,060
and conversion types, and these are
presented in tables in the Python Docs.

45
00:04:54,060 --> 00:05:01,540
So I'll just quickly load that up and
show you that link.

46
00:05:01,540 --> 00:05:06,360
So check this out for more information on this page.
Once again, though, I've mentioned these

47
00:05:06,360 --> 00:05:09,960
so that you'll understand them when reading
code from the internet, and if you end up

48
00:05:09,960 --> 00:05:15,030
using Python 2. You shouldn't be using
these in Python 3 code. Alright, so let's

49
00:05:15,030 --> 00:05:19,700
move onto the next video, which is a
quick summary of what we've been through in this section.

50
00:05:19,720 --> 00:05:22,640
See you in the next
video.

