Now, up to this point, we've learned that SDR.
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 and will be stored in a place in memory.
And after that place, a memory e will be stored.
Then the space will be stored.
And then m then e the space, then the ne.
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 valley.
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, 0 to 7.
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 going to grab from the bookshelf, index of zero all the way to
seven.
Very, very cool.
And this concept we're going to 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 going to 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 want to look at zero.
So we got in.
We wanted to look at seven.
We got eight.
But then there's an extra thing that we can do if we do a.
Colin 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.
01234567.
Just so it's easier to understand.
If I do, let's say zero.
Colin too.
And I click run.
I get start at zero and at two.
So I get to grab zero.
Two one.
Because remember I'm saying start here, but then stop as soon as you get to bookshelf, too.
We don't want that.
So if I do 0 to 7, 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 0 to 8 because there's actually a character
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 and 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 to grab two, then step over, one to 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, well, we'll happen.
Ready to guess?
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 going to 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 feel 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 to 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 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 because 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 want to 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 want to let say reverse an order.
And if we want to, 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.