Python Programming Tutorials - Slicing up Strings

in programming •  7 years ago  (edited)

Hello, well welcome to another Python tutorial. And today's tutorial we're going to talk about indexing and slicing a string in Python. In this one we're going to actually access the index and we're going to also go to slice our strings so we can grab from multiple parts of the index, so I just want you to see that little diagram again just to remind you of what we're talking about here, so let's create a string

python-logo.png

>>> a = "This is my string"
>>> a
'This is my string'
>>> 

a is equal to this is my drink and hit return so that's my string. Alright so when we want to get an index when we're indexing in a string we're actually going to be actually only pulling one odd character if you will from this string here okay, so remember T is zero and it counts up 0 1 2 3 4 5 6 7 8 9 10 11 and so on alright.

>>> a[0]
'T'
>>> 

So we're going to do a square brackets which allows us to access the which is indicate we're indexing, and we're going to say let's get 0 and then close out square brackets hit return and we get T alright. Let's so we got the zero index here this is index number.

>>> a[1]
'h'
>>> 

So let's do a square brackets, 1 square brackets hit return we get to H, since we went 0, 1 we got the H. On to do one more for practice a square brackets

>>> a[6]
's'
>>> 

let's do 6 return alright so we got s. So you know that's indexing in Python, so we can actually grab the one character out when indexing.

We can also go from the right from the left is positive numbers from the right is negative numbers, but don't get confused off from the left it always starts as 0 from the right it always starts at negative 1. So let's take a look at that,

>>> a[-1]
'g'
>>> 

a square brackets negative 1 square brackets return and we get negative 1 comes back with g, so like I said always going to write is negative numbers or accountant from the right hand side and negative 1 is our starting point, so negative 1 is g, and a string.

Let's take a look at one more

>>> a[-6]
's'
>>> 

a square brackets negative 6 square brackets if you return that's s. So that's indexing it's pretty simple pretty straight forward you just got to understand, from the left is positive numbers from the right is negative numbers, from the left it starts with 0 from the right starts with negative 1 alright.

If you can remember that got no problems with index, now let's take a look at slicing. Slicing allows us to grab a larger group of the string, so to do that we're going to add just 1 more limit to our index here it makes it allows us slice.

So what I'm going to do?, so first things first we're going to do

>>> a[1:5]
'his '
>>> 

a square brackets 1 and then when we do a colon it indicates for slicing, I'm going to do 5 and then square brackets hit return. What happened here alright so what happened here was we got we started at 1 which is the H is 0 1 it's H so that's our starting point right here is our starting point and our ending point is 5. So it's 5 where 5 is 0 1 2 3 4 5 5 is the I. Okay why are we showing the high here?, well our ending point in slicing always stop 1 point 1 position before where we coded it.

I hope that makes sense so I was trying to think of a way to explain this it always stops one number or one index location position before the index we indicate in our code alright. So let's take another look at another one here just for a little bit more practice.

>>> a[0:4]
'This'
>>> 

a let's do 0 and let's do four square bracket hit return, alright so here we got the full this. So like I said what our ending point is always going to end one position before the one we indicate in our code. Alright so that's slicing, let's take a couple more examples of slicing let's try one that looks like this

>>> a[2:]
'is is my string'
>>> 

a 2 is our starting point and then we to colon to say hey we're slicing and then I'm going to put a square bracket here and it returned, what happens here?, well we started the number two index position and you see in our string that's where it is is which is kind of looks funny is is this part is as part of this is is my string, so basically we're just given a starting point but not an ending point.

If you don't give an ending point it's going to get it to the end of the string and stop there. Let's take a look at another one

>>> a[:7]
'This is'
>>> 

a square brackets colon 7. So we're not giving it a starting point but we're giving an ending point hit return. So we're not given the starting point and it's always going to start at zeros default. So that's right because it always stops one position before we coded it for the ending point. Let's take a look at this one

>>> a[0:-1]
'This is my strin'
>>> 

a square bracket 0 negative 1 square bracket, here return and what happened, here well we're saying I started the zero index and go all the way up to negative 1 index, well negative one index is g in our case, g and we're saying that's where you want to stop. So remember wherever we want it to stop it's always one position before.

How about if we do

>>> a[:]
'This is my string'
>>> 

a square brackets colon square brackets, hit return what's going on here. Basically we're making a copy of our string object, given those starting and no ending so default is 0 and defaults ended a string for the ending very simple. So that is slicing.

Now we're going to talk about stepping which is part of slicing where it allows us to skip through characters. So we're going to do

>>> a[0:9:2]
'Ti sm'
>>> 

what's going on here?, so 0 we're starting at zero index we're ending at the 9 index but it will actually end at the 8 index, and we're skipping every other, hit return alright, so what happens here we go T, we skip H we go I you skip the S, we get the space, we skip the I, we get the s we skip the space we get the M and I'm guessing number arrow why is number 9 in our index, alright so that's how that works.

We could also take a look at something like this

>>> a[::3]
'Tssytn'
>>> 

a square brackets no default starting point no default ending point and we'll do count escaped by three returned and we get TSS YTN so what's it doing?, so we were doing T it skips 2 then we get to s, skip two more and then we get to s, skips two more and we get the Y, skips two more we've got the T and then skip some more with n. So the third it's saying skip by two.

Here's one for you how about if we do

>>> a[::-1]
'gnirts ym si sihT'
>>> 

a square bracket and we're going to do the same way no starting no ending negative one square bracket. What happens here?, well when we're looking at it we're going to say alright well it's probably going to go and skip backwards, well actually it's going to reverse our string, check that out. How about if we do

>>> a[::-2]
'git ms iT'
>>> 

a square brackets colon colon negative two square brackets, what's going on here well it's actually going to count backwards skipping are just going to skip backward. It's going to make our string backwards and skip skip every other one so hit returned. So that's what it does whatever the heck that spells so it's skipping backwards.

So that is stepping in Python if you give it a negative number it's going to go backwards to the string positive number it's going to go forward through the string by if you give it a two it's going to skip every other three it's going to go up by skip by two and so on.

One other way I want to show you the slice we could do something like this and I didn't show you this was it's you know something we don't use all that often

>>> a[slice(None,None,2)]
'Ti sm tig'
>>> 

So that is slicing indexing stepping and we also showed you another way to slice in Python if you have any questions leave a comment. I'll see you the next soon.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!