WEBVTT

1
00:00:01.180 --> 00:00:02.680
<v Instructor>Hi guys and welcome back.</v>

2
00:00:02.680 --> 00:00:04.220
In this video, we're going to talk about

3
00:00:04.220 --> 00:00:06.623
string formatting in Python.

4
00:00:07.480 --> 00:00:08.940
Specifically, we're going to be using

5
00:00:08.940 --> 00:00:13.340
a newer Python feature from Python 3.6 onwards.

6
00:00:13.340 --> 00:00:14.410
So for this video,

7
00:00:14.410 --> 00:00:18.590
make sure that you're using Python 3.6 or later.

8
00:00:18.590 --> 00:00:20.427
Let's say that you've got two strings,

9
00:00:20.427 --> 00:00:24.269
one, which is Bob, and has the name of 'name,'

10
00:00:24.269 --> 00:00:26.010
and one which is Hello, Bob and has the name of 'greeting.'

11
00:00:28.610 --> 00:00:30.964
If you print greeting out, of course

12
00:00:30.964 --> 00:00:32.796 line:15% 
you're going to get,

13
00:00:32.796 --> 00:00:37.420 line:15% 
Hello Bob, since that are the contents of the string.

14
00:00:37.420 --> 00:00:40.528
But if you set 'name' to Rolf

15
00:00:40.528 --> 00:00:43.380
and then you print greeting again,

16
00:00:43.380 --> 00:00:45.500
then naturally as you run that,

17
00:00:45.500 --> 00:00:49.420 line:15% 
you're gonna get Hello, Bob and then Hello, Bob again.

18
00:00:49.420 --> 00:00:53.950
Greeting contains the string name, which is Bob,

19
00:00:53.950 --> 00:00:57.320
but it doesn't calculate it dynamically,

20
00:00:57.320 --> 00:00:59.900
which means that when greeting is defined,

21
00:00:59.900 --> 00:01:04.900
it contains the characters Hello comma space Bob.

22
00:01:05.020 --> 00:01:07.970
And then changing 'name' doesn't change that greeting.

23
00:01:07.970 --> 00:01:10.550
So if you wanted to change the greeting,

24
00:01:10.550 --> 00:01:13.250
you would have to do something a little bit different.

25
00:01:15.160 --> 00:01:18.403
Indeed, instead of typing, Hello comma Bob,

26
00:01:18.403 --> 00:01:20.444
what you'd like to do is to put an 'F'

27
00:01:20.444 --> 00:01:22.980
in front of the quotation marks.

28
00:01:22.980 --> 00:01:26.610
So now we've got what's called an F string in Python.

29
00:01:26.610 --> 00:01:29.810
An F string allows us to embed variables

30
00:01:29.810 --> 00:01:31.560
inside of strings.

31
00:01:31.560 --> 00:01:33.700
So here instead of Hello comma Bob,

32
00:01:33.700 --> 00:01:38.700
we can do Hello comma 'name' inside these curly braces.

33
00:01:38.960 --> 00:01:42.249
Now, again, we've got F and then inside the string,

34
00:01:42.249 --> 00:01:46.200
we can use the curly braces and inside the curly braces,

35
00:01:46.200 --> 00:01:48.850
we can put a variable name.

36
00:01:48.850 --> 00:01:52.090
So when this is calculated,

37
00:01:52.090 --> 00:01:55.190
'name' will be replaced by Bob,

38
00:01:55.190 --> 00:01:57.830
so we will print out Hello, Bob.

39
00:01:57.830 --> 00:01:58.940
So if we run that,

40
00:01:58.940 --> 00:02:01.780 line:15% 
you'll see down here that we get Hello, Bob.

41
00:02:01.780 --> 00:02:04.640
So exactly the same thing as before.

42
00:02:04.640 --> 00:02:06.820
At this point, I'll say that if you do

43
00:02:06.820 --> 00:02:10.034
what we had earlier and set 'name' to Rolf,

44
00:02:10.034 --> 00:02:11.830
and then you print greeting again,

45
00:02:11.830 --> 00:02:14.440
you'll have the same problem as before.

46
00:02:14.440 --> 00:02:15.920 line:15% 
So if you press play again,

47
00:02:15.920 --> 00:02:18.100 line:15% 
you'll see Hello, Bob, Hello, Bob.

48
00:02:18.100 --> 00:02:21.250
So what we've done here is we have embedded

49
00:02:21.250 --> 00:02:24.160
the variable inside the string

50
00:02:24.160 --> 00:02:26.540
with the value that that variable had

51
00:02:26.540 --> 00:02:28.630
at this point in time, in line 2.

52
00:02:28.630 --> 00:02:31.280
So in line 2, 'name' had the value of Bob,

53
00:02:31.280 --> 00:02:33.343
so greeting is Hello, Bob.

54
00:02:34.410 --> 00:02:36.640
So what is the point of using the F string

55
00:02:36.640 --> 00:02:38.930
if it doesn't change dynamically?

56
00:02:38.930 --> 00:02:40.470
Well, the good thing about this F string

57
00:02:40.470 --> 00:02:42.940
is that instead of defining a variable for it,

58
00:02:42.940 --> 00:02:46.190
if you wanted to have a different value each time,

59
00:02:46.190 --> 00:02:48.713
you could just print it out, like that,

60
00:02:48.713 --> 00:02:51.716
and get rid of this variable definition.

61
00:02:51.716 --> 00:02:54.740
Now what we've got here is we are printing

62
00:02:54.740 --> 00:02:56.590
and inside the brackets, we've got

63
00:02:56.590 --> 00:02:58.060
the value that we want to print,

64
00:02:58.060 --> 00:02:59.485
so here we have the F string,

65
00:02:59.485 --> 00:03:02.300
which includes an F, then the quotation marks,

66
00:03:02.300 --> 00:03:04.725
and then the characters inside it, including the variable.

67
00:03:04.725 --> 00:03:06.512
By doing something like this,

68
00:03:06.512 --> 00:03:08.578
now every time you run this code,

69
00:03:08.578 --> 00:03:12.100 line:15% 
you're going to use the name at its latest point in time,

70
00:03:12.100 --> 00:03:15.113 line:15% 
so we get Hello, Bob, and Hello, Rolf.

71
00:03:17.060 --> 00:03:19.154
There is a way of defining a template,

72
00:03:19.154 --> 00:03:22.120
which instead of calculating it at

73
00:03:22.120 --> 00:03:23.860
the time where it is defined,

74
00:03:23.860 --> 00:03:25.330
you can calculate the values

75
00:03:25.330 --> 00:03:27.470
to be replaced inside it later on.

76
00:03:27.470 --> 00:03:28.950
Here's how you do that.

77
00:03:28.950 --> 00:03:31.738
So you can define your greeting as 'Hello,'

78
00:03:31.738 --> 00:03:34.620
and then open and closing curly braces.

79
00:03:34.620 --> 00:03:37.710
Notice that we don't have an F in front of it.

80
00:03:37.710 --> 00:03:39.890
Then you can say that with 'name,'

81
00:03:39.890 --> 00:03:41.250
which is another variable,

82
00:03:41.250 --> 00:03:45.728
is greeting.format and then 'name.'

83
00:03:45.728 --> 00:03:47.240
So what we've done here

84
00:03:47.240 --> 00:03:49.722
is we have created now three strings.

85
00:03:49.722 --> 00:03:52.560
We have the string 'name,' which has the value Bob,

86
00:03:52.560 --> 00:03:54.900
we have the string 'greeting,' which is our template,

87
00:03:54.900 --> 00:03:57.584
which says, "Hello comma curly braces

88
00:03:57.584 --> 00:04:01.690
and finally we say, greeting.format

89
00:04:01.690 --> 00:04:05.190
and then inside brackets, we put in the name.

90
00:04:05.190 --> 00:04:07.056
So what this is doing is essentially it's calling

91
00:04:07.056 --> 00:04:09.640
the format function

92
00:04:09.640 --> 00:04:13.320
inside of 'greeting' and what that's doing is,

93
00:04:13.320 --> 00:04:16.730
it is giving the name to this template,

94
00:04:16.730 --> 00:04:19.400
and what the template will do is it will

95
00:04:19.400 --> 00:04:20.810
replace the curly braces

96
00:04:20.810 --> 00:04:23.790
by the contents of the 'name' variable.

97
00:04:23.790 --> 00:04:27.890
So 'with_name' will contain "Hello comma Bob."

98
00:04:27.890 --> 00:04:30.981
If we print that out, you'll see what I mean.

99
00:04:30.981 --> 00:04:33.900 line:15% 
You get Hello, Bob printed out.

100
00:04:33.900 --> 00:04:37.750
One of the benefits of this is that you can then

101
00:04:37.750 --> 00:04:40.830
use this 'with_name' with other things,

102
00:04:40.830 --> 00:04:43.569
so you can reuse the template, the greeting,

103
00:04:43.569 --> 00:04:46.464
and instead pass in Rolf, for example,

104
00:04:46.464 --> 00:04:49.500 line:15% 
and then if you run this, you'll get Hello, Rolf.

105
00:04:49.500 --> 00:04:51.910
So it allows you to define a template

106
00:04:51.910 --> 00:04:54.630
that you can reuse with multiple values.

107
00:04:54.630 --> 00:04:58.530
Notice that in here you can have one with Bob there,

108
00:04:58.530 --> 00:05:00.560
then you can have one with Rolf

109
00:05:02.407 --> 00:05:05.360
and you can do that everywhere then.

110
00:05:07.860 --> 00:05:10.100 line:15% 
So now you have reused the template two times

111
00:05:10.100 --> 00:05:12.180 line:15% 
to create two different strings.

112
00:05:12.180 --> 00:05:15.540
This is why the format can be useful

113
00:05:15.540 --> 00:05:17.750
instead of F strings, but generally,

114
00:05:17.750 --> 00:05:19.990
you'll be using F strings

115
00:05:19.990 --> 00:05:22.233
if you're using Python 3.6 or later.

116
00:05:23.980 --> 00:05:26.550
Another benefit of using format

117
00:05:26.550 --> 00:05:29.140
is that you can create longer templates.

118
00:05:29.140 --> 00:05:30.920
For example, here we've got a longer phrase

119
00:05:30.920 --> 00:05:33.235
that says Hello comma placeholder.

120
00:05:33.235 --> 00:05:35.450
Today is placeholder.

121
00:05:35.450 --> 00:05:37.692
So we can say longer_phrase.format

122
00:05:37.692 --> 00:05:40.900
and then we can pass in Rolf and Monday.

123
00:05:40.900 --> 00:05:42.870
What this will do is it will put the values

124
00:05:42.870 --> 00:05:45.300
Rolf and Monday into the placeholders

125
00:05:45.300 --> 00:05:47.724
one at a time, so Rolf will go into the first placeholder,

126
00:05:47.724 --> 00:05:50.063
Monday will go into the second placeholder,

127
00:05:50.063 --> 00:05:51.802
and then you can print this out,

128
00:05:51.802 --> 00:05:55.610 line:15% 
and you will get back the appropriate value,

129
00:05:55.610 --> 00:05:57.323 line:15% 
Hello, Rolf. Today is Monday.

130
00:05:58.180 --> 00:06:00.478
Again, you could do this with many different strings,

131
00:06:00.478 --> 00:06:02.960
so that is something that is available

132
00:06:02.960 --> 00:06:04.160
if you'd like to use it,

133
00:06:04.160 --> 00:06:06.840
but generally you'll be using F strings.

134
00:06:06.840 --> 00:06:08.300
Those are the two main ways of

135
00:06:08.300 --> 00:06:10.840
doing string formatting in Python,

136
00:06:10.840 --> 00:06:14.822
which means, essentially, to embed values inside a string.

137
00:06:14.822 --> 00:06:17.100
There are other ways which are older,

138
00:06:17.100 --> 00:06:19.890
but we're not gonna be learning about those in these videos,

139
00:06:19.890 --> 00:06:22.713
just because they're not really used all that much anymore.

140
00:06:22.713 --> 00:06:24.400
Thanks for joining me in this video

141
00:06:24.400 --> 00:06:26.013
and I'll see you in the next one.

