Now up to this point we've learned that SDR is an ordered sequence of characters right.
It's essentially a piece of tax where we can write our names passwords paragraphs and sentences and
strings underneath the hood are stored in memory as I said as eight an ordered sequence of characters.
So for example if I had the string me me me then this will be stored in our computers 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 this space than and the need.
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 0 and
then e is stored in Location 1 and then space is stored in Location 2 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 0 2 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 it's 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.
OK 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 0.
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 0 and what's on that bookshelf of index of 0.
Well the letter and 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 am 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 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 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 it 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 him we want 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 zero one two three four five six seven
just so it's easier to understand.
If I do let's say the zero Colin too and I click Run I get.
Start at zero and 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 2.
We don't want that so if I do 0 2 7 What do you think will happen while I click Run and I get all the
way to 6 so in order for us to grab the full string we have to do 0 to 8 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 and here and then step over a few things.
So the default is 1 because we're going one by one through our bookshelf.
You see that we get the entire string but if I add to here it steps over by 2.
So I grab 0 then I step over 1 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 will happen.
Ready to guess.
One two three four five six seven this says Hey.
Start at 1.
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 OK.
What if I do something like this where I don't feel the start and do five here.
What happens now.
I get zero one two three four.
All the way until five.
So it starts as default zero and goes to five OK.
What if I do something like this to semicolons and then when 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.
Mm 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 you see that we get the reverse of the string.
So this is a very useful notation if you want to let's say reverse an order and if we want to let's
say skip by two click Run and you see that we're skipping by two now now hopeful 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.