Strings are what are called an iterable, and they are a sequence,
meaning that we can break them apart, and we can take action
based on every single letter in a string.
So with that comes this idea of an index, and we can do this
with a list or an array or a tuple as well.
So let's go ahead and make a string.
So the 'course = "Python for Everybody".
And an index is literally just a number.
So it's your variable name, and then it's got the hard brackets,
and then you just put a number in there.
So number 0, because computers always start counting at
0, is going to be the 'P', and then we can just simply count from there. So
if we go 0-1-2-3-4-5, the fifth letter in here, the fifth
number rather is going to be the 'n'.
So let's go ahead and test out these assumptions.
So index 0, again, that's just with your variable name,
hard bracket, then your index number, and then closing hard
bracket. 'P', just as expected.
And 'course[]', what I say it was? 5, I believe, is going to
be 'n', just like that.
So in most programming languages an index tends to start
with the number 0, not all of them, but most of them.
So to humans, 0 really just doesn't mean anything.
0 actually means literally nothing.
And it's kind of crazy to think about,
but to a computer, 0 is a number.
So if I said you have 5 apples and I have 0 apples,
well, I have nothing.
I have a basket of nothing.
It's just an empty basket of nothing.
You might have a basket of apples.
I have a basket of zero apples, aka a blank or empty or void
basket. But to a computer, there is always that 0 number,
and that's why computer languages usually start an index with the number
0, it's because it's actually, it's a real thing for a computer.
So that is indexing through a string.
If we want to index through a list, we could do 'lst', short for list
Because if I do 'list', you can see it sort of glows green a little bit,
that is syntax highlighting.
That means it's a reserved keyword.
So let's just do 'lst = ['Car', 'Truck', 'Airplane', 'Blimp']'
I don't know why I'm using any of these in this example,
but let's take a look at this anyways.
So I have this variable called 'lst' short for list.
And if this follows the exact same pattern as up here, 'Car'
will be 0, 'Truck' will be 1, 'Airplane' will be 2, and
'Blimp' will be 3.
And we can actually test out our assumptions here.
So we can do 'list', index 0, 'list[0]'
And that's not going to work because I actually typed 'list',
it should be 'lst[0]', and there we go,
we got 'Car'. 'lst[2]'.
Just take a sec, and think about this one. Which one is going to be 2?
[no audio]
If you are thinking that index 2 is going to be 'Airplane',
you're absolutely correct.
If you think index 2 is going to be 'Truck', well remember
we start counting at 0, 0-1-2.
And if you've made that sort of mistake already, that is
okay, because I've been doing this for 20 years and indexing
still occasionally it still comes out and bites me as well.
So it's just something to sort of wrap your head around.
But we can see that 'lst[2]' is 'Airplane'.
Now, just for funsies, let's go ahead and see if we can get the reverse.
So let's do list with a negative number.
Which one do you think this is going to provide
if we start counting backwards from 0? This one is 'Blimp'.
negative one, negative 2, negative three, negative 4.
Let's try that again.
'lst[-3]'.
And if this holds up, -1, -2, -3, should be 'Truck'.
And there we go.
So now we have indexing forwards and backwards.
Now that's just part of indexing.
The other part of indexing is this thing called slicing.
So if I type out the word 'course' here, we have "Python for
Everybody". In a string we can slice very easily.
All we have to do is say, 'course', where do we want to
start? Let's start at the very beginning. That's the 'P'.
And for how long do we want to go for, or up until rather?
And let's say we want to go up until the number 5.
So 0-1-2-3-4-5.
Let's take a look at this.
Well, this one only gives us "Pytho", not "Python".
So if we wanted the whole word, we would have to use 'course
[0:6]',
and that will give us the word "Python".
But let's also, let's slice the word 'for' out of here.
So we've got 0-1-2-3-4-5-6.
So we have to start on index 7, 8-9-10.
So let's do 'course[7:10]'
Let's see what this gives us.
Gives us the word 'for', and that's slicing a string.
Now we also have that 'lst', right,
and we can slice a list as well.
So let's slice this list. Let's do 'lst',
let's grab number 1 and go up until number 3, 'lst[1:3]
So 0-1-2-3, we're going to start at 1, 0-1.
We're going to start at 'Truck',
we're going to grab 'Airplane' as well,
and we're going to go up to the third index.
So we're not going to get 'Blimp', but we're just going to
get up until 'Blimp'.
So 'Truck' and 'Airplane' is what we should be getting back.
And sure enough, there we go.
We have now indexed and sliced a string and a list, so sequences.
Last but not least, we can also take these slices or
any of these indexes, and we can put them into another variable.
So we have this variable called 'course',
but what if we wanted to get a variable called 'language'?
'language = course[,
start at 0,
go to six,
hit 'Enter',
[no audio]
and there we go.
We have 'language' as "Python",
and 'course' is still "Python for Everybody".
So all it did there was it took out this little piece of text,
and threw it into a new variable called 'language'.
Now, in most Python cases, at least Python cases I run into
you won't use this one too often, but you will use list slicing quite a bit.
So even though slicing or indexing a particular string or
a sentence, well it's not actually super useful, it is good to
know because you can also apply it to lists,
you can apply it to tuples and things like that.