1
00:00:04,570 --> 00:00:08,680
When printing strings and numbers, it
would often be convenient to display

2
00:00:45,700 --> 00:00:51,780
So let's swing over to the code and create a new file.

3
00:00:51,780 --> 00:00:57,760
I'm going to call this one repfields.

4
00:00:57,760 --> 00:01:03,360
Alright, so I'm going to start by typing age equals 24, then on line 2 we're going to do print parentheses

5
00:01:03,360 --> 00:01:13,400
double quotes, my age is, space double
quote + str age in parentheses +

6
00:01:13,400 --> 00:01:19,120
double quotes space years, closing double
quote and right parenthesis.

7
00:01:19,120 --> 00:01:25,840
So if we run the program, we confirm we get the output, my age is 24 years, and no longer

8
00:01:25,840 --> 00:01:30,040
getting an error now, when we're using
the plus operator to concatenate numbers

9
00:01:30,040 --> 00:01:37,480
and strings. So using the string function
helps us to, basically, coerce an integer

10
00:01:37,480 --> 00:01:41,830
into a string, but this can rapidly
become tedious; having to coerce every

11
00:01:41,830 --> 00:01:46,270
variable using the string function.
Python 3 provides a much better

12
00:01:46,270 --> 00:01:53,320
mechanism, and that has improved further in Python 3.6. So python 3 allows strings

13
00:01:53,320 --> 00:01:59,380
to be formatted using replacement fields
and the dot format method. And these are

14
00:01:59,380 --> 00:02:04,200
probably best explained with a few
examples. So let's change line 2,

15
00:02:04,200 --> 00:02:09,139
instead of using the string function, I'm
going to put my age is, and I'm going

16
00:02:09,139 --> 00:02:13,100
to put left and right curly braces
before the closing double quote,

17
00:02:13,100 --> 00:02:16,120
with a zero in between them.

18
00:02:16,120 --> 00:02:19,640
So my age is, space left and right curly braces,

19
00:02:19,640 --> 00:02:25,420
zero between that and a space years. I'm going to
delete all the code up until the right

20
00:02:25,420 --> 00:02:33,940
parenthesis, and replace outside the
double quote with dot format age in parentheses.

21
00:02:33,940 --> 00:02:37,090
So this will produce the
same output as the previous example,

22
00:02:37,090 --> 00:02:41,320
but without having to coerce the number into a string. So let's just run this

23
00:02:41,320 --> 00:02:46,720
to confirm that it works. We get the same
output; my age is 24 years.

24
00:02:46,720 --> 00:02:51,780
So the replacement field is represented by the
left curly brace the 0 and the right

25
00:02:51,790 --> 00:02:56,680
curly brace, which is then replaced by
the first value in the format list,

26
00:02:56,680 --> 00:02:59,590
age in this case. And at the moment we've only got the one value in there and

27
00:02:59,590 --> 00:03:03,519
that's the variable age. So that doesn't
look like much of an improvement over

28
00:03:03,520 --> 00:03:09,880
the previous code, but consider this one
instead; so I'm going to type it out.

29
00:03:09,880 --> 00:03:16,140
Line 4, I'm going to type print parentheses
and double quotes, there are, and we'll add

30
00:03:16,150 --> 00:03:19,930
some replacement fields. So first one
I'm going to do is a zero and get all of

31
00:03:19,930 --> 00:03:26,000
these in curly braces, There are zero
days in, then a curly brace again, 1

32
00:03:26,000 --> 00:03:31,720
with curly braces comma, and we'll do the same for 2, 3, 4, 5, 6 and 7.

33
00:03:31,720 --> 00:03:41,940
2, 3. 4, 5, and you can
see I'm putting commas and putting curly

34
00:03:41,940 --> 00:03:45,360
braces around these numbers again, 6
and we'll finish off the last one.

35
00:03:45,360 --> 00:03:49,320
We'll put and, replacement field 7.

36
00:03:49,320 --> 00:03:52,300
So I'm going to do that, then press enter there.

37
00:03:52,300 --> 00:03:58,520
On the next line, type dot format, then another set of parentheses,

38
00:03:58,520 --> 00:04:04,880
and I'm going to type 31 comma in the 
left parenthesis, well after the left parenthesis

39
00:04:04,880 --> 00:04:09,880
Jan, March, or M a r, the abbreviations I'm  going to use here,

40
00:04:09,880 --> 00:04:15,500
May, oops, I missed a double quote there.

41
00:04:16,459 --> 00:04:26,760
July, August, October, and the last one, December.

42
00:04:26,770 --> 00:04:31,630
Then a closing parentheses. So I've split that
code over two lines, which is something

43
00:04:31,630 --> 00:04:36,220
you'll see done a lot in Python. Long
lines are actually frowned upon in the

44
00:04:36,220 --> 00:04:40,000
Python style guide, which we'll talk more
about later. For now, just know that

45
00:04:40,000 --> 00:04:43,800
Python allows you to split your lines of
code like I've just done.

46
00:04:43,800 --> 00:04:48,700
Alright, so the replacement fields are replaced by the
values that appear in the dot format

47
00:04:48,700 --> 00:04:53,710
method call, with the first value
replacing 0, the second replacing 1 and

48
00:04:53,710 --> 00:04:56,710
so on. So you can see there, we've actually got a total of 7 replacement fields

49
00:04:56,710 --> 00:04:59,950
defined on line 4, and each of those
values are going to go into the

50
00:04:59,950 --> 00:05:06,280
respective number, 0 through 7. So we've
got a total of 8 items in the list after

51
00:05:06,280 --> 00:05:10,630
the dot format, and each of those will go
into the replacement fields numbered 0

52
00:05:10,630 --> 00:05:17,900
through 7 in our string. So if we run the program now,

53
00:05:17,920 --> 00:05:19,500
and comparing the output at

54
00:05:19,510 --> 00:05:23,350
the bottom of the screen with the values
that appear inside the parentheses

55
00:05:23,350 --> 00:05:26,880
after the dot format method call.

56
00:05:56,300 --> 00:05:58,900
Of course, you probably wouldn't use string literals

57
00:05:58,900 --> 00:06:02,640
inside format, because of course they can
just be included in the string.

58
00:06:02,640 --> 00:06:07,280
So we could have, in this case, replaced what we did there, with something that's probably a

59
00:06:07,280 --> 00:06:20,260
lot more readable; print There are, replacement field 0 days in, in Jan,

60
00:06:20,260 --> 00:06:32,420
March, May, July, August, October and December and
dot format.

61
00:06:32,420 --> 00:06:38,120
So that does exactly the same thing, but I just wanted to demonstrate that you can have multiple replacement fields

62
00:06:38,120 --> 00:06:41,930
inside a string, and we'll just run that
to confirm, and I probably should have

63
00:06:41,930 --> 00:06:48,720
written it like that, and to be consistent,
run it again and we've got the identical output.

64
00:06:48,720 --> 00:06:52,040
The other thing to keep in mind is that
fields can be used more than once,

65
00:06:52,040 --> 00:06:55,970
and they don't have to appear in the order
that the values are provided to the dot

66
00:06:55,970 --> 00:06:59,300
format method call.
It's the field index, the number inside

67
00:06:59,300 --> 00:07:03,480
the curly braces, that determines which
value to be used.

68
00:07:03,480 --> 00:07:07,120
So here's an example to demonstrate that.

69
00:07:07,120 --> 00:07:13,040
So I'm gonna come down here on line 8, print parentheses,

70
00:07:13,040 --> 00:07:15,780
I'm gonna start with a double quote Jan:

71
00:07:15,780 --> 00:07:27,060
I'm gonna start with replacement field 2,
then Feb: 0 replacement field, Mar:

72
00:07:27,060 --> 00:07:38,680
I'm gonna use 2 again, Apr: with
replacement field 1, May: 2, Jun: 1,

73
00:07:38,680 --> 00:07:54,420
replacement field 1, Jul: replacement field 2, Sep: 1 and Oct: 2,

74
00:07:54,420 --> 00:08:03,720
replacement field 2, two more, Nov: 
replacement field 1 comma Dec: replacement field 2.

75
00:08:03,720 --> 00:08:05,800
and obviously, I've got a large
font here so we're not gonna be able to

76
00:08:05,810 --> 00:08:10,400
see the entire contents on that line, on
this one line. So what I'm going to do is

77
00:08:10,400 --> 00:08:16,400
actually go to the end there, press enter
there, dot format, and I'm going to put

78
00:08:16,400 --> 00:08:20,880
three values; 28, 30 and 31.

79
00:08:20,880 --> 00:08:25,460
So if we run the program, and you can see now that

80
00:08:25,460 --> 00:08:30,950
wherever there's a replacement field 0,
it's replaced with the first value in

81
00:08:30,950 --> 00:08:35,900
the list, 28. We've only got replacement
field 0 once for February,

82
00:08:35,900 --> 00:08:40,440
and all the replacement field 1 fields are
replaced with the second value, which is 30,

83
00:08:40,440 --> 00:08:46,980
and the third value, 31, is used to replace all the replacement field 2 replacement fields.

84
00:08:46,980 --> 00:08:50,120
Now you can also use
triple quotes. What I'll do is I'll print

85
00:08:50,120 --> 00:08:56,540
a blank line first, and close this run
window down. We'll just print an empty line,

86
00:08:56,540 --> 00:09:01,180
and we can also do something similar
using triple quotes. So I could do print

87
00:09:01,180 --> 00:09:05,040
parentheses three quotes, triple quotes
in other words,

88
00:09:05,040 --> 00:09:09,520
press ENTER there. Then what I'm going to
do is take a copy of this entire line,

89
00:09:09,520 --> 00:09:15,440
just save me typing it again. Okay, take a
copy of that. I'm going to go back and

90
00:09:15,440 --> 00:09:20,360
paste it after the triple quotes. Then
what I'm going to do is just press ENTER

91
00:09:20,360 --> 00:09:24,050
after each one to make it a bit more
readable, and because I'm pressing enter

92
00:09:24,050 --> 00:09:26,920
on a separate line now, I'm also going
to remove the double quotes,

93
00:09:26,920 --> 00:09:34,860
remove the comma rather. So we've got February, March.

94
00:09:44,420 --> 00:09:48,480
That's the end of the line now, so I'm going to add the three closing double quotes there,

95
00:09:48,480 --> 00:09:51,860
and I'm just going to put our
format statement after that, dot format

96
00:09:51,860 --> 00:10:02,280
parenthesis 28, 30, 31, like so.
So now if we run that,

97
00:10:02,280 --> 00:10:06,860
I'm looking at the output for this program. Each month is appearing on a new line and the

98
00:10:06,860 --> 00:10:11,040
number of days is also showing on there as well.
So that, I'm sure we agree, would be very

99
00:10:11,040 --> 00:10:14,720
difficult, or would be very messy, to
reproduce with string concatenation.

100
00:10:14,720 --> 00:10:19,500
So the actual replacement is pretty easy to
work out.

101
00:10:22,480 --> 00:10:27,180
So replacement field zero is
replaced with 28, and you can see the example there,

102
00:10:27,180 --> 00:10:31,540
showing us on line 14. So it's automatically coerced to be a string for us.

103
00:10:31,540 --> 00:10:37,060
Replacement field 1 is replaced by 30.
We've defined that as the second entry

104
00:10:37,060 --> 00:10:41,440
in our format list there, and replacement
 field 2 is replaced by 31.

105
00:10:41,440 --> 00:10:45,060
You can see, obviously, the same use of the
replacement fields in the code

106
00:10:45,060 --> 00:10:49,060
and we can see the output showing down the
bottom of the screen. Now if we'd used

107
00:10:49,060 --> 00:10:52,870
replacement field 3 in the string, then
we'd have to produce, or provide rather,

108
00:10:52,870 --> 00:10:57,880
another value in the dot format method
call. Alright so make sure you understand

109
00:10:57,880 --> 00:11:03,600
how the replacement fields are being
used before moving on to the next video.

