-: Now up to this point, we've learned
that str,
is an ordered sequence of characters, right?
It's essentially a piece of text
where we can write our names, passwords,
paragraphs and sentences.
And strings underneath the hood are stored in memory,
as I said, as an ordered sequence of characters.
So for example,
if I had,
the string, me, me, me,
then this will be stored in our computer's memory,
in an order.
So M will be stored in a place in memory,
and after that place in memory,
E will be stored,
then the space will be stored,
and then M, then E, then space, then M, then E.
So if you think of a bookshelf,
each one of these is ordered
in a different part of the bookshelf, but one after another.
And the way we can think about this, is like this.
The M is stored in location zero,
and then E is stored in location one,
and then space is stored in location two,
and so, on and so forth.
And each shelf, now corresponds
to,
a value.
So that, when we ask the computer,
"Hey, I want you to grab this string",
We know that, hey,
we're going to look in the shelf zero to seven.
Now, why is this important?
Because one of the most useful things
you can do with strings,
is to access different part of a string
using its, what we call, an index.
So for example,
when you have a string, and let's call this variable,
or this piece of string, selfish.
And selfish has me, me, me, me, me.
And I'm going to comment this out,
just like this,
Make sure it's aligned, okay?
And what I'm going to do here, is I'm going to say
selfish, and then square brackets right here,
and then type in zero.
Now, if I print this,
and I click run,
check that out.
I get the letter M.
Why is that?
Well, using the square brackets,
I'm telling the computer,
"Hey, grab the variable selfish."
And then from that variable selfish,
"grab whatever is an index of zero."
And what's on that bookshelf of index of zero?
Well, the letter M.
What if I do seven?
What do you think will happen?
-: If I click run, I get E.
Because on the bookshelf of number seven, that's E.
And when I'm getting just selfish,
I get the entire string.
It's going to say,
"all right, I'm gonna grab from the bookshelf,
index of zero all the way to seven."
Very, very cool.
In this concept, we're gonna explore throughout the course,
and you're going to see
why it's important very soon.
But using this knowledge we can do a lot
of string manipulation.
And Python has this unique feature,
that we're gonna talk about right now,
that allows us to grab different pieces of text.
So let's have a look here.
The rule here, is when we use square brackets in Python,
the first item that we put between the square brackets,
is what we call the start.
The start is, "Hey, where do you want me to look?"
In our case, we said we wanna look at zero.
So we got M.
We wanted to look at seven, we got E.
But then there's an extra thing that we can do.
If we do a colon here,
we also have the option,
to say, "Hey, where to stop?"
What does that mean?
Well, if I do something,
and you know what let's change this to
zero, one, two, three, four, five,
six, seven,
just so it's easier to understand.
If I do, let's say zero,
colon,
two,
and I click run,
I get start at zero end at two.
So I get to grab zero,
to one.
Because remember, I'm saying start here,
but then stop as soon as you get to bookshelf two.
We don't want that.
So if I do zero to seven,
what do you think will happen?
Well, I click run,
and I get all the way to six.
So in order for us to grab the full string,
we have to do zero to eight.
Because there's actually eight characters here, right?
Because we start from zero.
If I click run, there you go.
Now, there's a third thing that we can add here.
And this, is called the step over.
And the step over says,
"Hey, start here, end here,
and then step over a few things."
So the default, is one,
because we're going one by one,
through our bookshelf.
You see that we get the entire string.
But if I add two here,
it steps over by two.
So I grab zero, then I step over,
one, two, grab two,
then step over, one, two, grab four,
step over, and so on and so forth.
So, let me ask you a couple of tricky questions here.
What happens if I do this?
Is this valid?
If I click run, what will happen?
Ready to guess?
I get one, two,
three, four, five, six, seven.
This says, "Hey, start at one."
So that is right here,
but then afterwards,
stop, well,
there's nothing there.
So the default is gonna say, go all the way to the end.
I know this is confusing, but hang in there.
Practice this often enough,
and this will become second nature.
Okay, what if I do something like this?
Where I don't fill the start,
and do five here.
What happens now?
Ready?
I get zero, one, two, three, four.
All the way until five.
So, it starts as default zero, and goes to five.
Okay, what if I do something, like this?
Two semicolons, and then one.
If I click run,
all right, I get the default behavior.
Because it's starting at zero, when there's nothing.
It ends at whenever the string ends,
when there's nothing, and then we're stepping over
through it once.
All right, but what if we do something like this?
What if I do minus one here?
Hmm, this is a tricky one.
What will happen, let's find out.
I get seven.
In Python, the negative index,
means, "hey, start at the end of the string."
So that if I do minus two,
I get six, If I do minus three,
I get five.
'Cause I'm going backwards.
And a neat trick that you can do here,
is if we do semicolon, semicolon,
minus one,
and this is actually quite a common operation,
so you just have to memorize this even though
it looks weird,
it means start, stop,
there's no limit here,
but I wanna step over from the back.
So what do you think will happen here?
If I click run?
Do you see that?
We get the reverse, of the string.
So this is a very useful notation,
if you wanna, let's say, reverse an order.
And if we wanna, let's say, skip by two,
I click run, and you see that we're skipping, by two now.
Now, hopefully your head doesn't hurt
with all this notation.
You just have to get used to it.
It's something quite unique to Python,
but it's very, very useful.