1
00:00:00,930 --> 00:00:02,220
-: Welcome back.

2
00:00:02,220 --> 00:00:05,460
So far we've learned a few built-in functions

3
00:00:05,460 --> 00:00:07,290
that Python has.

4
00:00:07,290 --> 00:00:09,450
Few built-in actions, right?

5
00:00:09,450 --> 00:00:11,520
Actions that we can take on data.

6
00:00:11,520 --> 00:00:13,800
And we learned about SDR.

7
00:00:13,800 --> 00:00:16,350
We learned about INT.

8
00:00:16,350 --> 00:00:18,480
We learned about Float

9
00:00:18,480 --> 00:00:20,730
and the Type conversion that we can do.

10
00:00:20,730 --> 00:00:22,920
We also learned about Type.

11
00:00:22,920 --> 00:00:23,753
We also learned

12
00:00:23,753 --> 00:00:28,680
about Print and these built in functions in Python.

13
00:00:28,680 --> 00:00:32,369
Well, there's a few of them and I'll link to this resource

14
00:00:32,369 --> 00:00:35,160
but there's not too, too many.

15
00:00:35,160 --> 00:00:37,080
As a matter of fact, we've also seen ones

16
00:00:37,080 --> 00:00:42,080
like ABS or Round in our Numbers video.

17
00:00:46,080 --> 00:00:51,080
So Numbers had some functions that we can use,

18
00:00:51,150 --> 00:00:52,383
some built-in functions.

19
00:00:53,836 --> 00:00:57,600
Well, Strings also has a very useful one called Len

20
00:00:57,600 --> 00:01:00,750
which stands for length.

21
00:01:00,750 --> 00:01:04,140
So as you guessed it, if I type

22
00:01:04,140 --> 00:01:05,310
in something like this

23
00:01:05,310 --> 00:01:09,870
and I print this out and I click run

24
00:01:09,870 --> 00:01:11,310
I get nine.

25
00:01:11,310 --> 00:01:13,440
Because it's calculating the length

26
00:01:13,440 --> 00:01:15,060
of the string in our case.

27
00:01:15,060 --> 00:01:20,060
1, 2, 3, 4, 5, 6, 7, 8, 9.

28
00:01:22,380 --> 00:01:25,980
The length of the string is nine.

29
00:01:25,980 --> 00:01:27,780
Now, you have to be careful here

30
00:01:27,780 --> 00:01:30,810
because the length itself doesn't start at zero

31
00:01:30,810 --> 00:01:32,490
as we've seen with indexes.

32
00:01:32,490 --> 00:01:35,940
Instead, it counts like humans do from one.

33
00:01:35,940 --> 00:01:40,653
So a neat thing to do here is I can do something like this.

34
00:01:42,000 --> 00:01:46,403
Let's say that we have a variable Greet that equals hello.

35
00:01:49,140 --> 00:01:53,193
Well, I can grab the Greet variable,

36
00:01:54,870 --> 00:01:55,830
which is a string

37
00:01:55,830 --> 00:02:00,830
and use string slicing to grab, well, the first

38
00:02:01,950 --> 00:02:04,530
and then go all the way until the end.

39
00:02:04,530 --> 00:02:06,690
Now, the default already does this.

40
00:02:06,690 --> 00:02:11,690
If I leave it like this, it's going to grab hello.

41
00:02:12,648 --> 00:02:14,396
Oh, and make sure I add a bracket here.

42
00:02:14,396 --> 00:02:15,229
Let's click run.

43
00:02:15,229 --> 00:02:16,062
I get hello.

44
00:02:17,400 --> 00:02:22,260
Now, if I do zero to length of greet

45
00:02:24,652 --> 00:02:25,860
What does that mean?

46
00:02:25,860 --> 00:02:30,360
Well, if I click run, I still get hello, because the length

47
00:02:30,360 --> 00:02:33,933
of Greet is nine.

48
00:02:37,020 --> 00:02:37,853
Like so.

49
00:02:38,963 --> 00:02:42,750
Now, this length function is quite useful

50
00:02:42,750 --> 00:02:45,300
and we're going to use it a lot in this course.

51
00:02:45,300 --> 00:02:48,000
But I want to introduce to you the concept of built-in

52
00:02:48,000 --> 00:02:53,000
functions as well as what we call built-in methods.

53
00:02:54,510 --> 00:02:56,373
Hmm, what does that mean?

54
00:02:58,407 --> 00:03:00,060
A built-in function had this syntax

55
00:03:00,060 --> 00:03:02,940
of the word that was highlighted in blue

56
00:03:02,940 --> 00:03:07,940
and then we used curly brackets to perform some action

57
00:03:08,460 --> 00:03:10,740
on a data type.

58
00:03:10,740 --> 00:03:14,340
However, Python also has this idea of methods.

59
00:03:14,340 --> 00:03:17,130
And methods are similar to functions,

60
00:03:17,130 --> 00:03:20,850
but they they are owned by something.

61
00:03:20,850 --> 00:03:25,680
So for example, in Python we have string methods.

62
00:03:25,680 --> 00:03:27,330
So these are methods

63
00:03:27,330 --> 00:03:31,710
or actions that only strings can perform.

64
00:03:31,710 --> 00:03:33,240
And don't worry, this is something

65
00:03:33,240 --> 00:03:34,680
that we'll talk more about

66
00:03:34,680 --> 00:03:37,860
when we talk about classes and functions.

67
00:03:37,860 --> 00:03:42,000
But for now, Python, for example, has string methods.

68
00:03:42,000 --> 00:03:43,560
So these are methods

69
00:03:43,560 --> 00:03:46,861
that we can use specifically for strings.

70
00:03:46,861 --> 00:03:49,300
And methods have a special syntax

71
00:03:50,324 --> 00:03:52,908
where instead of just the word with the curly brackets,

72
00:03:52,908 --> 00:03:55,803
it usually has a dot in front of it.

73
00:03:56,919 --> 00:03:59,400
So dot, and then some sort of word.

74
00:03:59,400 --> 00:04:02,490
And we've actually seen one format, right?

75
00:04:02,490 --> 00:04:07,490
Dot format, curly brackets or brackets is a method.

76
00:04:07,560 --> 00:04:11,190
And if I go to Python string methods and I scroll down

77
00:04:11,190 --> 00:04:13,890
or I scroll up, we see that format.

78
00:04:13,890 --> 00:04:17,523
Format specific values in a string is a method.

79
00:04:18,660 --> 00:04:20,910
Now, why do we care?

80
00:04:20,910 --> 00:04:25,910
Well, with this Python gives us automatic tools

81
00:04:26,040 --> 00:04:27,990
that we can use on strings.

82
00:04:27,990 --> 00:04:31,083
So let's explore some of these string methods.

83
00:04:32,310 --> 00:04:34,503
Let's create a quote here.

84
00:04:35,959 --> 00:04:38,853
And the quote is going to be to be or not to be.

85
00:04:40,920 --> 00:04:43,233
Now in here with this quote,

86
00:04:44,314 --> 00:04:47,430
we can use some methods on this string.

87
00:04:47,430 --> 00:04:49,540
Now, I'm not gonna go through everything

88
00:04:50,490 --> 00:04:52,620
because, well, 60% of these you're most

89
00:04:52,620 --> 00:04:55,440
likely never going to use in your career.

90
00:04:55,440 --> 00:04:57,570
I'm gonna go over the important ones that you'll see

91
00:04:57,570 --> 00:05:00,000
over and over again, but I'll also link to this

92
00:05:00,000 --> 00:05:02,520
so that you can see this for yourself as a reference.

93
00:05:02,520 --> 00:05:06,010
Remember, as a programmer your job is not to memorize this

94
00:05:07,051 --> 00:05:09,420
and read a language like you read a dictionary.

95
00:05:09,420 --> 00:05:12,930
Instead it is to know that this resource exists

96
00:05:12,930 --> 00:05:15,270
and it's for you to explore

97
00:05:15,270 --> 00:05:18,870
and learn as a specific problem occurs.

98
00:05:18,870 --> 00:05:21,033
But let's get back to the task at hand.

99
00:05:22,650 --> 00:05:27,197
One thing that we can do is to use the upper method.

100
00:05:28,620 --> 00:05:31,332
We use a dot and then type in upper

101
00:05:31,332 --> 00:05:34,200
and then the curly brackets.

102
00:05:34,200 --> 00:05:37,950
And luckily for us, our editor shows us

103
00:05:37,950 --> 00:05:39,840
that this actually exists

104
00:05:39,840 --> 00:05:41,610
because of the dropdown menu.

105
00:05:41,610 --> 00:05:43,350
So what happens now?

106
00:05:43,350 --> 00:05:44,943
If I print this,

107
00:05:46,119 --> 00:05:48,393
and I click run,

108
00:05:50,130 --> 00:05:50,963
look at that.

109
00:05:50,963 --> 00:05:53,040
Everything gets capitalized.

110
00:05:53,040 --> 00:05:54,513
To be or not to be.

111
00:05:56,520 --> 00:06:01,520
There is also another one we can use called Capitalize.

112
00:06:02,910 --> 00:06:05,640
What's the difference between the two?

113
00:06:05,640 --> 00:06:08,250
Well, if I run, you see

114
00:06:08,250 --> 00:06:12,063
that it capitalizes the beginning of the sentence.

115
00:06:13,470 --> 00:06:16,050
And by the way, your editor, or in our case, our repl

116
00:06:16,050 --> 00:06:17,610
if you hover over this

117
00:06:17,610 --> 00:06:20,643
it'll actually tell you what it does.

118
00:06:21,780 --> 00:06:24,933
It returns a capitalized version of S.

119
00:06:26,280 --> 00:06:27,990
So whatever is to the left

120
00:06:27,990 --> 00:06:28,823
of the dot.

121
00:06:30,150 --> 00:06:32,910
And a good editor will actually

122
00:06:32,910 --> 00:06:35,640
show you all the things available to you.

123
00:06:35,640 --> 00:06:39,000
So as soon as you press a dot, look at that.

124
00:06:39,000 --> 00:06:41,670
I see all these purple boxes

125
00:06:41,670 --> 00:06:45,990
which are methods that are available to me for a string.

126
00:06:45,990 --> 00:06:48,660
And you see there's a lot of them.

127
00:06:48,660 --> 00:06:49,650
Now, don't worry

128
00:06:49,650 --> 00:06:53,010
about these double underscores, these dunder methods.

129
00:06:53,010 --> 00:06:55,080
Because this is something that we'll talk

130
00:06:55,080 --> 00:06:56,910
about when we talk about classes.

131
00:06:56,910 --> 00:06:58,328
But you can see

132
00:06:58,328 --> 00:07:01,170
that you have different things that you can use.

133
00:07:01,170 --> 00:07:03,362
For example, you have lower instead of upper

134
00:07:03,362 --> 00:07:05,763
it lower cases everything.

135
00:07:07,860 --> 00:07:09,990
What if I want to use find?

136
00:07:09,990 --> 00:07:13,288
For example, and find simply says

137
00:07:13,288 --> 00:07:18,288
hey, does B exist in the code to be or not to be?

138
00:07:19,113 --> 00:07:24,113
And if I click run, well, it tells me that yes.

139
00:07:25,680 --> 00:07:30,633
Quote dot find has a B, and it starts at index of 3.

140
00:07:31,650 --> 00:07:36,650
So if I go to 0, 1, 2, and then look at that 3.

141
00:07:37,170 --> 00:07:41,490
So we have find, which finds us the first occurrence

142
00:07:41,490 --> 00:07:43,683
of a piece of text.

143
00:07:45,150 --> 00:07:49,410
You also have things like Replace, where I can replace

144
00:07:49,410 --> 00:07:54,270
whatever I give it as the first thing, and then separate it

145
00:07:54,270 --> 00:07:55,443
to the second thing.

146
00:07:56,451 --> 00:08:00,210
So you can see over here it tells me old and then comma new.

147
00:08:00,210 --> 00:08:03,390
And I can replace this with me.

148
00:08:03,390 --> 00:08:05,090
If I click run,

149
00:08:05,090 --> 00:08:08,220
I get to me or not to me.

150
00:08:08,220 --> 00:08:12,390
So this replaces all the occurrences of be.

151
00:08:12,390 --> 00:08:16,710
Now, the final thing I wanna point out to you is that

152
00:08:16,710 --> 00:08:19,473
if I print here, quote,

153
00:08:20,790 --> 00:08:23,250
what do you think is going to happen?

154
00:08:23,250 --> 00:08:28,230
I've printed here and I replaced to me, to be

155
00:08:28,230 --> 00:08:30,700
or not to be with to me or not to me

156
00:08:32,620 --> 00:08:34,049
quite a selfish quote.

157
00:08:34,049 --> 00:08:36,960
But if I print this again on the fifth line

158
00:08:36,960 --> 00:08:38,789
what do you think will happen?

159
00:08:38,789 --> 00:08:39,623
Let's find out.

160
00:08:41,700 --> 00:08:42,533
Whoa.

161
00:08:43,799 --> 00:08:45,483
Is this what you expected?

162
00:08:46,440 --> 00:08:48,510
Think about why this might happen based

163
00:08:48,510 --> 00:08:49,743
on what we've learned.

164
00:08:51,180 --> 00:08:52,013
Here's the thing.

165
00:08:52,013 --> 00:08:54,810
Remember, strings are immutable

166
00:08:54,810 --> 00:08:57,120
That is, they cannot be changed.

167
00:08:57,120 --> 00:09:01,500
We can override them if we want, but we don't change them.

168
00:09:01,500 --> 00:09:03,660
We either create them or destroy them.

169
00:09:03,660 --> 00:09:06,480
In our case, when we do quote

170
00:09:06,480 --> 00:09:09,097
dot replace to me or not to me,

171
00:09:09,097 --> 00:09:12,183
it's creating a new string.

172
00:09:13,080 --> 00:09:16,590
Now, we're not assigning this string to anything.

173
00:09:16,590 --> 00:09:20,640
So eventually after we print it, we remove it from memory.

174
00:09:20,640 --> 00:09:25,020
But if I do something like this

175
00:09:25,020 --> 00:09:27,630
where I have quote

176
00:09:27,630 --> 00:09:32,630
two equals quote, dot replace and I print quote two.

177
00:09:37,157 --> 00:09:40,832
Well in our case, we're creating a whole new string.

178
00:09:40,832 --> 00:09:41,665
We're creating a string.

179
00:09:41,665 --> 00:09:44,250
But we never modify the original string

180
00:09:44,250 --> 00:09:46,200
because it's immutable.

181
00:09:46,200 --> 00:09:51,200
So that quote always stays the way it is.

182
00:09:51,300 --> 00:09:52,500
Until we destroy it.

183
00:09:52,500 --> 00:09:55,410
Until we remove it, our program ends.

184
00:09:55,410 --> 00:09:57,153
This is going to exist.

185
00:09:58,230 --> 00:10:00,630
All right, hope you're having fun.

186
00:10:00,630 --> 00:10:03,363
Hang in there more to learn in the next video.

