Hi again, in the previous lecture we talked about strings and in this lecture
again we are going deeper into learning strings and specifically we're going to add
indexing. Indexing is a very important concept not only with strings but
with all the data type that we'll be looking at later such as lists,
tuples and dictionaries, so please follow this lecture. Yeah, let's talk about
indexing. Let me open the Python interactive session. Let me create a
string, "Hi there!" exclamation mark and close quote.
Now when you create a string, every string that you create Python under the hood
what it does is it assigns a number to each or the items of your string so it
starts from 0 so H will be assigned an index of 0 and then a 1 for I, 2 for the
space, 3 for T, five, six seven, eight.
So the exclamation mark character will will have an index of eight, so you would
ask why is this useful? Well this allows you to extract certain parts from a
string and there is a certain notation to do that you'd want to access the string.
So in this case that's variable C holding the string, then you want to use
square brackets so an opening square bracket and then a closing square
bracket and inside those square brackets you pass the index of the item that you want
to extract. Let's say I want the T character so that would be 0 1 2 3.
I pass number 3 there as an index and that will extract for me the letter T which is
actually a string. So you can check the type of the
output and you'll see that that's a string, so a string is made of strings.
You can say so. If you wanted H you'd past zero and if you wanted a lost
character well you might have to count so one two three four five six seven
eight. And so, and sorry including type there.
Sorry about that! so c8 would give you the exclamation
mark, but there's also another indexing system there that Python uses so in this
case we had to count from the beginning to the end of the string but wouldn't it
be useful if you had another system that lets you count from the end, so from
right to left? Well, such a system exists and it
starts from minus 1, so the exclamation mark would be minus 1 now that you can
extract that with that. Minus 2 would be E, R would be minus 3 and so on up to H, so we
have two indexing systems there which allow you to extract items depending on
whether the items are in the beginning or near the end. Now what if you want to
extract more than one item from a string? Let's say you want to extract Hi.
Well there is a notation for that too, so Hi is expanding from index 0 to index 1.
So up to here. So let's try 0 to 1. That will not work a very good, but let's see.
So as I said this will return only H, so it doesn't return item with index 1.
That's because splitting in Python, so we we're splitting a string here
splitting in Python is upper bound exclusive, which means that the upper
bounds of the split here is not included in the output and so if you want to
include I you want to pass the index after I which is two so this is the string
just for reference, so we extracted item with index 0 and item with index 1.
And then the split starts right there at index 2 so it doesn't include the space.
Just hi. If you pass 3 there, that would include the wide space as well and
similarly you can pass like one. That will include the I and the space, there
are also shortcuts, so if you pass zero like nothing to 3 but that is the same
as doing 0 to 3 so it will include 0 1 & 2 and similarly if you want to extract
there with the exclamation mark well what you'd want to do is T would
have an index of 0 1 2 3 so 3 and then you can just pass a column there and
execute it and that will give you There with the exclamation mark. Let's now try
to extract R and E using negative indexing, so something to know is that
even though we are using a negative indexing which starts from right to left
the splitting will work from left to right which means the first index you
want to pass is that of the first item. So R we pass minus 3 for R because
you know we have minus 1, minus 2 and minus 3. So minus 3 for R and then we want E
which is minus 2, but since splitting is upper bounce
exclusive we want to pass minus one there. So let's try that, minus one, execute and yeah
we get the correct output. That's the idea. If you didn't pass
anything here you'd get the exclamation mark as well. So it will take a while
until we get used to this, but with some practice you'll be able to remember
these notations. Yeah, that's what I wanted to cover in this lecture, so this was
about string indexing and splitting, so you split using the index that Python
uses on the background. Yeah, I hope you enjoyed this and I'll talk to you
later, thanks!