We can access a wide range of characters using slicing
if we want to cut a substring from a string.
This is known as String slicing.
We can access a range of items in a string by using the slicing operator colon and this is how the syntax
was like.
Thing looks like say for example I want to retrieve the characters you in the from these string country.
This is how we construct the slice the first index number is where these lines start.
So we want to begin the slicing at the character you which is inclusive.
And the second index numbered is where these lies and exclusive.
So when I say the second index number is 6 these lighting stops here
and it is going to exclude the character.
Why.
That is the index number 6.
So the output would be you in T.
Let's check these lighting functionality in the Jupiter non book.
Let's define a string and like we discussed we will
we will slice the string from the index to up to index six exclusive.
So as you can see here the original string is d r 1 has been sliced from index to 2 to index 6 excluding
now.
Say for example I would want to retrieve all of the characters in the string then I could give something
like this.
If we remove both of the indexes and include the colon in between these great brackets this would retrieve
all of the characters in this string.
So this would give me on the characters.
Now if I want to retrieve say for example characters up onto index code that gives I can leave the first
index as blank or Z2 and then the output would be C or U N because this exclude the index numbered form
which is d next if I want to retrieve the characters beginning from the card index number.
In that case I can give the first index as 2D and leave the second index as blank.
This would give me t y.
So let me see this in the in the Jupiter note book.
So in order to retrieve all of the characters in this string within the square brackets I include the
colon that the paused and the second index numbers as blank so this retrieves all the characters.
Now save for example exam.
I want to retrieve all of the characters beginning from the index.
Three.
So I would give you the index three here as the first index number and leave out the second index number.
So slicing begins with the index number three and retrieves all of the characters until the end of the
string.
Say now I want to retrieve the characters beginning from the zero 8 index up until the index by so nutcase
I am going to leave out the first index number and enter the second index number six and instead of
four leaving the first index number as blank.
You can also enter zero meaning you're on to the tree beginning from the zero it index.
So the result remained the same.
Another feature of slicing is extended slicing.
Say for example I would like to retrieve every other character the index number zero up until the index
number five.
So I want to retrieve the characters see
and deep.
So how do we do that and this is this index for the extended slice.
The first index numbered repos to where the slicing begins.
The second index number refers to where the slicing ends and deterred but amateur hood is known as a
straight.
And this refers to how many characters to move forward after the first character is retrieved from the
string.
So in either case we are going to retrieve every other character every second character from the first
character.
This would give me the output as I see you deep by default the.
Is this parameter would be 1 which means it retrieves all of the characters beginning from 0 with index.
Let's go see how the extended slice works in the Jupiter known book.
So this is a string.
String 1.
And I'm going to begin the slicing from index 0 so begins things.
It is index 0.
You can either enter 0 or leave it as a blank.
The second index number is 5 so these lighting and add 5 character play and the third parameter to this
trait parameter and we are going to enter 2 which means we are going to retrieve every other character.
So executing.
So this is the output.
Let us see.
I want to retrieve all of the characters between 0 and 5.
And I'm going to enter these tried but amateur as one execute so it doesn't read all of the characters.
Now let us skip over these trade parameters so enter your first and second index numbers so the output
is the same as this.
Then this trade parameter has been given as one.
This means Python defaults to these tried of one so that every character between the two index numbers
is retrieved.
This is about slicing.