1
00:00:05,250 --> 00:00:09,900
Welcome back, everyone, to the series of lectures on Python basics in this lecture, we're talking

2
00:00:09,900 --> 00:00:10,680
about strings.

3
00:00:12,080 --> 00:00:18,260
Strings in Python are used to hold text information and are indicated with the use of single or double

4
00:00:18,260 --> 00:00:18,710
quotes.

5
00:00:19,250 --> 00:00:22,280
We can also think of a string as a sequence of characters.

6
00:00:22,430 --> 00:00:27,140
And this is actually a super important idea the python, because this means there's actually an order

7
00:00:27,230 --> 00:00:27,980
to the string.

8
00:00:29,180 --> 00:00:32,680
So an example string would be something like ABCD PFG.

9
00:00:32,720 --> 00:00:34,790
So seven letters or seven characters.

10
00:00:35,150 --> 00:00:40,040
But what's really interesting here is Python understands that a is the first character of the string

11
00:00:40,280 --> 00:00:43,730
and pythons actually going to apply an index with each character.

12
00:00:43,760 --> 00:00:45,740
And it actually starts the index at zero.

13
00:00:46,130 --> 00:00:48,860
So, for example, the letter A is at index zero.

14
00:00:49,070 --> 00:00:51,500
The Letter B is an index one and so on.

15
00:00:51,770 --> 00:00:57,290
So you can actually use this indexing to grab particular elements or characters from that string.

16
00:00:58,850 --> 00:01:04,610
We're also going to be able to take what are known as slices of this string using bracket notation so

17
00:01:04,610 --> 00:01:10,790
we could grab multiple characters in a row if we wanted to grab ABCD, for instance, we could actually

18
00:01:10,790 --> 00:01:14,210
use bracket notation for that and we'll show you how to do that later on in the lecture.

19
00:01:15,690 --> 00:01:21,300
We should also note that strings are just Python objects or data structures and their methods, we can

20
00:01:21,300 --> 00:01:23,790
actually call directly from the string to transform it.

21
00:01:23,970 --> 00:01:28,620
For example, there's a method to uppercase every letter in this string, and we'll explore that as

22
00:01:28,620 --> 00:01:28,920
well.

23
00:01:30,590 --> 00:01:35,900
Now, the ideas of indexing, slicing and method calls are actually going to apply to many other objects

24
00:01:35,900 --> 00:01:37,370
and Python will be discovering.

25
00:01:37,610 --> 00:01:39,980
So I want you to keep that in mind as you learn them for strings.

26
00:01:40,430 --> 00:01:44,840
Well, are going to learn some things that are very specific towards strings, but then indexing slicing

27
00:01:44,840 --> 00:01:47,720
a method calls will be able to use that later on for other objects as well.

28
00:01:48,080 --> 00:01:50,390
OK, let's get started exploring strings in Python.

29
00:01:50,540 --> 00:01:51,950
I'm going to head over to our code editor.

30
00:01:52,430 --> 00:01:54,890
All right, so here I am inside our code editor.

31
00:01:55,250 --> 00:02:00,080
Let's go ahead and just quickly explain the use of single and double quotes within a Python string,

32
00:02:00,500 --> 00:02:03,500
so we can say Python string with single quotes.

33
00:02:03,500 --> 00:02:10,580
So I can say hello and single quotes like this, or I can also use double quotes to have that string

34
00:02:10,580 --> 00:02:10,850
there.

35
00:02:11,240 --> 00:02:15,920
Keep in mind that if I were to run this example python file right now, it's if I were to say Python

36
00:02:16,580 --> 00:02:18,510
example that Python go ahead and run that file.

37
00:02:18,530 --> 00:02:19,670
I don't see any output.

38
00:02:19,910 --> 00:02:25,490
That's because to actually see the output using the command line, I would need to actually print one

39
00:02:25,490 --> 00:02:26,120
of these strings.

40
00:02:26,600 --> 00:02:28,340
So now I can save that, run it.

41
00:02:28,590 --> 00:02:29,630
Now I see the output.

42
00:02:29,960 --> 00:02:30,260
OK.

43
00:02:30,680 --> 00:02:33,770
So single quotes or double quotes doesn't really matter.

44
00:02:34,040 --> 00:02:38,860
The one part where it actually may matter is if you actually want to essentially encapsulate something

45
00:02:38,870 --> 00:02:40,760
of a single quote or a double quote.

46
00:02:41,150 --> 00:02:45,770
So, for example, let's imagine the sentence I don't want to go.

47
00:02:46,310 --> 00:02:48,590
So this is a single quote, but it's just floating there.

48
00:02:48,590 --> 00:02:49,850
It's actually part of the word.

49
00:02:50,210 --> 00:02:55,170
What you can do with Python is you simply wrap it around the other type like this.

50
00:02:55,190 --> 00:02:56,210
I don't want to go.

51
00:02:56,630 --> 00:02:59,570
And let's actually cut that and put it inside our print function.

52
00:03:00,680 --> 00:03:06,290
And then I can run this in Python says, OK, I understand that this single floating single quote here

53
00:03:06,440 --> 00:03:08,160
is OK and it actually prints out.

54
00:03:08,180 --> 00:03:08,900
I don't want to go.

55
00:03:09,260 --> 00:03:12,800
So that's how you can deal with double quotes or single quotes that are inside something.

56
00:03:12,920 --> 00:03:18,710
You can just keep wrapping it around another set of quotes to actually understand or have Python understand,

57
00:03:18,980 --> 00:03:21,350
which is the quote and which is kind of the string quote.

58
00:03:22,010 --> 00:03:23,930
So what else can we do with strings?

59
00:03:24,290 --> 00:03:29,570
There's a bunch of different method calls you can use on a string to then actually change the string.

60
00:03:29,600 --> 00:03:31,730
You can also concatenate strings for this sort of thing.

61
00:03:32,030 --> 00:03:33,440
We're actually going to create a variable.

62
00:03:33,830 --> 00:03:39,110
So remember, I can assign variables by saying something like my variable name is equal to.

63
00:03:39,470 --> 00:03:42,800
And then let's go ahead and have it be a string such as.

64
00:03:44,360 --> 00:03:47,900
Hello, and let's go ahead and explore just a couple of methods on this.

65
00:03:48,260 --> 00:03:49,340
So I'm going to say Prince.

66
00:03:50,750 --> 00:03:51,470
My variable.

67
00:03:52,490 --> 00:03:53,880
And so when I read this, I should see.

68
00:03:54,230 --> 00:03:54,620
Hello.

69
00:03:55,190 --> 00:03:58,130
Now what are method calls on objects in Python?

70
00:03:58,460 --> 00:04:05,900
Method calls is when you have a variable like my variable here, and it's a specific data type within

71
00:04:05,900 --> 00:04:06,440
Python.

72
00:04:06,800 --> 00:04:08,120
You can call Dot.

73
00:04:08,660 --> 00:04:12,890
And then what's going to happen is there's a bunch of different methods you can actually call, which

74
00:04:12,890 --> 00:04:14,510
are going to affect the object.

75
00:04:14,720 --> 00:04:18,290
Later on, when we learn about object oriented programming, we're going to discover how to create our

76
00:04:18,290 --> 00:04:19,070
own methods.

77
00:04:19,430 --> 00:04:22,370
Now let me show you an example of one of these methods.

78
00:04:22,670 --> 00:04:24,860
There's a ton of them, and we won't use all of them.

79
00:04:25,070 --> 00:04:29,360
But I do want you to be aware of the fact that these methods exist on objects.

80
00:04:29,570 --> 00:04:33,080
They're essentially just transformations or augmentations you can perform on an object.

81
00:04:33,410 --> 00:04:35,820
So a really obvious one is something like upper.

82
00:04:36,390 --> 00:04:42,680
Now, often I see a student mistake is they just call the actual method, but they don't execute the

83
00:04:42,680 --> 00:04:44,160
method to execute the method.

84
00:04:44,180 --> 00:04:48,380
You have to actually call open and close parentheses on that method and add a little bit of spacing

85
00:04:48,380 --> 00:04:48,590
here.

86
00:04:48,590 --> 00:04:49,790
Just so it's clear what I'm doing.

87
00:04:50,120 --> 00:04:55,820
I'm calling my variable the method name, which sometimes you have to look up on the internet or a documentation

88
00:04:55,820 --> 00:04:57,140
to understand what this method does.

89
00:04:57,560 --> 00:05:02,240
And then you have open and close parentheses to actually execute or run that method.

90
00:05:02,660 --> 00:05:06,680
So upper, as the name implies, will actually uppercase every single letter here.

91
00:05:07,130 --> 00:05:07,910
So we're going to save that.

92
00:05:09,340 --> 00:05:12,280
Run that and now notice hello, isn't all uppercase.

93
00:05:12,850 --> 00:05:18,820
There's also things like capitalize, capitalize, this kind of similar just uppercase is the first

94
00:05:18,910 --> 00:05:21,130
one, so we save that change.

95
00:05:21,790 --> 00:05:23,350
Run that and then we see hello.

96
00:05:23,740 --> 00:05:29,530
Always remember that to actually execute methods, you have to actually call the parentheses if you

97
00:05:29,530 --> 00:05:30,370
just do this.

98
00:05:31,290 --> 00:05:36,720
And forget to do those princes and you run this, it doesn't actually give you an error, it just says,

99
00:05:36,750 --> 00:05:40,560
oh, there's a built in method that's going to capitalize a string object.

100
00:05:40,830 --> 00:05:43,170
And it's this memory location on your computer.

101
00:05:43,500 --> 00:05:45,060
Let me know when you want to execute it.

102
00:05:45,120 --> 00:05:48,490
So Python actually is pretty helpful in its kind of report to you.

103
00:05:48,490 --> 00:05:51,960
It just says, OK, that's a capitalization method call.

104
00:05:52,200 --> 00:05:55,590
It's on this object that lives at this part in your computer and memory.

105
00:05:57,030 --> 00:06:01,110
I should point out later on, we're going to learn about something called attributes, which are more

106
00:06:01,110 --> 00:06:05,910
characteristics of the actual object rather than transformations on the object.

107
00:06:05,910 --> 00:06:08,940
And those won't have open close princes on them.

108
00:06:09,150 --> 00:06:11,160
But for right now, you can kind of ignore those.

109
00:06:11,190 --> 00:06:15,330
This is all going to make a lot more sense when we actually create our own objects with object oriented

110
00:06:15,330 --> 00:06:15,900
programming.

111
00:06:15,930 --> 00:06:20,220
But I just want you to be aware of the fact that there's method calls you can call on a string.

112
00:06:20,790 --> 00:06:24,450
The last one, I want to show you something called split, which is actually going to create what's

113
00:06:24,450 --> 00:06:26,800
known as a list, which we're going to learn about next.

114
00:06:26,820 --> 00:06:28,380
But let me just make a little string here.

115
00:06:29,220 --> 00:06:34,440
I'm going to make a sentence that says something like, Hello, Mike, how is your dog?

116
00:06:35,100 --> 00:06:35,430
OK?

117
00:06:35,880 --> 00:06:40,250
So I can see I have a long string here and this is under my variable.

118
00:06:40,260 --> 00:06:45,720
And then I'm going to call Dot Split Open close parentheses with that split.

119
00:06:45,750 --> 00:06:52,980
This is by default, it's going to split this string into multiple components, depending on what you

120
00:06:52,980 --> 00:06:55,320
pass in for, what you should split on.

121
00:06:55,800 --> 00:06:58,500
So by default, it's going to split on blank spaces.

122
00:06:58,710 --> 00:06:59,550
So if I run this?

123
00:07:01,090 --> 00:07:08,470
It says, OK, here are all the single words inside that string, and it returns them as a list, so

124
00:07:08,470 --> 00:07:09,700
there's bracket notation here.

125
00:07:09,940 --> 00:07:12,850
This is actually a list in Python where it learn about it next.

126
00:07:12,870 --> 00:07:17,950
So just keep in mind that Split returns a list and you can split on whatever you want.

127
00:07:18,370 --> 00:07:23,590
So for example, if there was a comma here that said, Hello, Mike Comma, how is your dog?

128
00:07:24,040 --> 00:07:29,890
I could say split on the string comma note how I'm passing the string here with a comma.

129
00:07:30,190 --> 00:07:32,080
And this just tells Python, Go ahead.

130
00:07:32,080 --> 00:07:37,750
And instead of default, splitting on the white or essentially blank components here, the little spaces

131
00:07:38,080 --> 00:07:39,670
split only on commas.

132
00:07:39,850 --> 00:07:42,220
So use commas as your splitter.

133
00:07:42,760 --> 00:07:46,630
Now, if I save that change and I run this, I see.

134
00:07:46,870 --> 00:07:49,930
Hello, Mike, split into the first one and then how is your dog?

135
00:07:50,530 --> 00:07:54,850
The other thing I could do is maybe split on letters, so maybe I could split on the O's.

136
00:07:55,120 --> 00:07:58,420
Kind of a weird choice here, but it is possible so I can run this.

137
00:07:58,840 --> 00:08:04,540
And now I've split on the Oh, so every time there's an o, i go ahead and chop off the O and split

138
00:08:04,900 --> 00:08:09,880
so I can see how those chopped off mike space h o was shut off.

139
00:08:10,210 --> 00:08:14,710
Then W is y o shut off, etc. You kind of get the idea here.

140
00:08:14,770 --> 00:08:20,380
This will be really useful later on when we have to quickly maybe grab all the words in a sentence or

141
00:08:20,380 --> 00:08:21,040
something like that.

142
00:08:21,310 --> 00:08:26,260
So often you're just using the default, which is blank just to grab all the words in a sentence.

143
00:08:27,220 --> 00:08:32,470
OK, so we have the ability to run methods on our actual strings.

144
00:08:32,860 --> 00:08:38,409
The other thing I want to point out is we have the ability to index particular items from a string.

145
00:08:38,919 --> 00:08:46,330
So for example, let's imagine I wanted to grab h this very first h that is capitalized on the string.

146
00:08:46,750 --> 00:08:51,940
There's something called index notation and bracket notation that we can perform on objects to grab

147
00:08:51,940 --> 00:08:52,750
indexed items.

148
00:08:52,870 --> 00:08:59,530
The way this works is after the variable name you have open and close square brackets and then you're

149
00:08:59,530 --> 00:09:04,520
going to pass in the number of the index location of the letter you want.

150
00:09:04,540 --> 00:09:11,920
Indexing starts at zero, so this is zero one two three four, etc. So if I say for my variable index

151
00:09:12,190 --> 00:09:15,080
zero, that should return the very first letter in that string.

152
00:09:15,880 --> 00:09:17,860
So I run that and I see now, h.

153
00:09:18,460 --> 00:09:21,820
Let's go ahead and try to grab em this capital M.

154
00:09:22,070 --> 00:09:24,550
Well, how many index locations is that over?

155
00:09:24,550 --> 00:09:24,980
Remember?

156
00:09:25,210 --> 00:09:28,960
A string is a sequence of characters, and you should also note that spaces count as a character.

157
00:09:29,320 --> 00:09:34,540
So this is going to be zero one two three four five and then six.

158
00:09:35,080 --> 00:09:39,040
So if I now pass in six here, what's there we go.

159
00:09:39,340 --> 00:09:42,220
So five percent six for my index location.

160
00:09:42,820 --> 00:09:44,170
It grabs M.

161
00:09:45,010 --> 00:09:48,970
I should also note that indexing can technically go backwards as well.

162
00:09:49,390 --> 00:09:55,180
For example, if I wanted to grab that very last g instead of having to count all the way forward,

163
00:09:55,480 --> 00:09:58,320
I could starting at zero count backwards.

164
00:09:58,330 --> 00:10:03,470
So if h0, that means if I start going backwards in the string, this will be negative one.

165
00:10:03,490 --> 00:10:04,870
This g was going to be negative one.

166
00:10:06,180 --> 00:10:09,210
So we're going to see negative one here, save that change.

167
00:10:10,600 --> 00:10:12,660
Now I can see there is a dog.

168
00:10:13,200 --> 00:10:18,450
So again, indexing can go forward for positive numbers or backwards for negative numbers.

169
00:10:18,720 --> 00:10:23,310
Often we'll use negative one as a quick way to grab the last item in a sequence.

170
00:10:24,450 --> 00:10:26,430
Now that's indexing for a single item.

171
00:10:26,790 --> 00:10:29,820
There is also the ability to grab slices, as we mentioned.

172
00:10:30,180 --> 00:10:33,780
So let's imagine I want to grab the entire slice of hello.

173
00:10:33,800 --> 00:10:35,700
I want to grab that word using slicing.

174
00:10:35,940 --> 00:10:37,770
What's the actual syntax for that?

175
00:10:38,220 --> 00:10:45,330
Well, you say you're starting index points, colon and then the index you want to go up to, but not

176
00:10:45,330 --> 00:10:45,840
include.

177
00:10:46,260 --> 00:10:50,550
So it's inclusive of the starting point and it's exclusive of the end index point.

178
00:10:51,030 --> 00:10:58,620
So that means I'm going to say starting at zero go zero one two three four and then I'd have to go up

179
00:10:58,620 --> 00:11:03,920
to five because I'm grabbing everything up to, but not including that end index point.

180
00:11:03,930 --> 00:11:06,930
So I pass and five there save that change.

181
00:11:08,020 --> 00:11:09,460
Run that, and now I get.

182
00:11:09,850 --> 00:11:10,270
Hello.

183
00:11:10,930 --> 00:11:16,000
Let's imagine if I were to try to pass in for super common beginner mistake is they say, OK, let me

184
00:11:16,000 --> 00:11:16,720
just cut the letters.

185
00:11:17,020 --> 00:11:19,150
Zero one two three four.

186
00:11:19,390 --> 00:11:24,130
But what happens is the way the Python interprets this as you want everything up to, but not including

187
00:11:24,130 --> 00:11:24,460
four.

188
00:11:24,790 --> 00:11:26,410
So if you run that, you'd only get back.

189
00:11:26,710 --> 00:11:27,110
Oops.

190
00:11:27,130 --> 00:11:28,210
I forget to save that change.

191
00:11:28,420 --> 00:11:30,490
OK, so now if you run that, you only get back.

192
00:11:31,000 --> 00:11:31,330
Hell.

193
00:11:31,780 --> 00:11:32,110
OK.

194
00:11:32,440 --> 00:11:36,100
So again, this happens because it goes up to but not including that last index point.

195
00:11:36,610 --> 00:11:38,070
So this is slice notation.

196
00:11:38,080 --> 00:11:44,350
It's your starting point colon and then your endpoint that it goes up to, but not including the endpoint.

197
00:11:45,130 --> 00:11:49,330
The last thing I want to note here is the ability to actually use f string literals.

198
00:11:49,660 --> 00:11:57,880
So if string literals the way they work is, they're actually going to be able to insert different variables

199
00:11:57,880 --> 00:11:58,780
into a string.

200
00:11:59,230 --> 00:12:02,650
This is not super useful for us right now, but later on, when we learn about functions, it's going

201
00:12:02,650 --> 00:12:04,480
to be super useful to just insert strings.

202
00:12:04,840 --> 00:12:10,720
So, for example, we could be creating a website that actually takes in names and passwords, so I

203
00:12:10,720 --> 00:12:12,820
could say something like the first name.

204
00:12:13,750 --> 00:12:14,500
It was a.

205
00:12:15,870 --> 00:12:16,770
And the last name.

206
00:12:18,310 --> 00:12:19,270
His Porsche here.

207
00:12:20,380 --> 00:12:24,730
And then let's imagine I wanted to print out something like, Hello, first name, last name.

208
00:12:25,570 --> 00:12:29,260
So I say hello, and then I have two options here.

209
00:12:29,560 --> 00:12:32,250
I could manually type in Jose Porteus.

210
00:12:32,260 --> 00:12:34,600
I could say something like, Hello, Jose Portillo.

211
00:12:34,960 --> 00:12:40,930
But maybe this is part of a larger program where I want this to just work for anybody, not specifically

212
00:12:40,930 --> 00:12:47,920
for Jose Portillo, because once this changes to something like Mimi Portillo, then this is hardcoded

213
00:12:47,920 --> 00:12:48,820
in to be Jose.

214
00:12:49,180 --> 00:12:53,650
So what I want to figure out is how can I just by changing what's up here?

215
00:12:53,950 --> 00:12:57,610
Have the print statement reflect any changes in first name and last name?

216
00:12:58,090 --> 00:13:00,010
So if we run this right now, I get back.

217
00:13:00,040 --> 00:13:00,970
Hello, Jose Bautista.

218
00:13:01,300 --> 00:13:06,460
But I want to actually use these variable calls, so I only need to change what's up here once in case

219
00:13:06,490 --> 00:13:08,750
maybe I have something like prints.

220
00:13:09,490 --> 00:13:15,940
Last name is 40A, so I have to keep hard coding stuff in which is not what I want it to be flexible

221
00:13:15,940 --> 00:13:16,840
to these variable names.

222
00:13:17,170 --> 00:13:19,240
The way we do that is with f string literals.

223
00:13:19,570 --> 00:13:23,440
What you do is two steps one at the start of the string.

224
00:13:23,440 --> 00:13:30,160
Outside the quotes, you just put an F. There this f is just to tell Python, Hey, keep a lookout for

225
00:13:30,160 --> 00:13:33,580
variables that I'm going to put in curly braces inside the string.

226
00:13:34,240 --> 00:13:39,400
And as I mentioned then, the second step is wherever you have something you want to insert, you put

227
00:13:39,400 --> 00:13:43,630
in curly braces and then you're going to put in the actual variable name instead.

228
00:13:44,140 --> 00:13:51,790
So I can say hello variable first name space, and let's replace portela here with curly braces variable.

229
00:13:52,810 --> 00:13:58,120
Last thing, and then we're going to do the exact same thing here and to replace Tortilla here with

230
00:13:58,120 --> 00:13:59,830
variable last name.

231
00:14:00,340 --> 00:14:01,400
Save those changes.

232
00:14:01,480 --> 00:14:05,410
And now after you save those changes and run this, you'll see the exact same results.

233
00:14:05,440 --> 00:14:09,460
Hello, Joseph TRIA, last name is Tortilla, but now let's imagine up here, maybe in this form or

234
00:14:09,460 --> 00:14:11,820
something, I change this to.

235
00:14:13,090 --> 00:14:15,400
Mike Smith, save those changes.

236
00:14:15,700 --> 00:14:21,100
Now this is going to automatically update with just those variable names, so it's only dependent on

237
00:14:21,100 --> 00:14:23,560
the variable names, which you can then change up here.

238
00:14:23,920 --> 00:14:26,080
So now when you say and run, you see.

239
00:14:26,110 --> 00:14:28,150
Hello, Mike Smith, last name is Smith.

240
00:14:28,720 --> 00:14:32,320
Very useful, especially for inserting things that you don't know ahead of time.

241
00:14:32,320 --> 00:14:35,710
A lot of times you're not going to know ahead of time what the exact first name and last name is.

242
00:14:35,920 --> 00:14:39,970
Instead, it's going to be like user defined on the web page, so it's going to be really easy to then

243
00:14:39,970 --> 00:14:42,010
just insert it based off the variable names.

244
00:14:42,550 --> 00:14:44,080
OK, so that's it.

245
00:14:44,410 --> 00:14:47,050
The things we talked about here were just basics of strings.

246
00:14:47,140 --> 00:14:47,770
Different methods.

247
00:14:47,770 --> 00:14:52,480
You can call in strings indexing and slicing on strings and then print formatting on strings.

248
00:14:52,900 --> 00:14:54,400
I'll see you at the next lecture.

