Enticing “Slicing”

Sandeshgaikwad
3 min readSep 16, 2023

--

Hello all, Sandesh here again,
In last article we saw what variable is.and also we have seen the different examples of it.If anyone wants to read the recent article about same, please refer below link.
https://medium.com/@sandeshgaikwad14/easy-understandable-variable-acdc52ebf58b
In this article we will learn what is ‘slicing’

What is slicing?
The term slicing itself showing its character,means slicing is nothing but, extracting a piece or some part from the whole object.So by using slicing input in the python,we can print the desierd part from the sequence.
Example;
lets consider we have varibale name which assign value is ‘mathematics’ but we want to print only ‘hema’. for this purpose we use slicing method.

When we assign any string value to the variable,bydefaut python gives negative and positive indexing to that value.
1]Positive indexing always starts from left and runs towards right.
2]the positive indexing is always starts from ‘0'.(0,1,2,3…)
3]Negative indesxing is always starts from right and runs towards left.
4]negative always starts from -1 (-1,-2,-3..)
Refer following image.

How to do slicing?

By using simple syntax
syntax
print(name[start index:stop index])
or
name[start index:stop index]

Lets see some examples for this.

In this image we can see, we assign a value ‘mathematics’ to the ‘name’ variable.by using syntax we give start and end index to get output.in input [18] we didn’t put any end index even though it prints value,without giving any error, means python itself interpreted to print all the the values till the end.Same with input [19].
in [20] we put end index 8 but it prints up to 7th character. Because end index is always -1.

Lets see some negative indexing with example.

Here we can see in [27] we used both negative and positive index in syntax, still it gives output.But this scenario will change when step by index comes in.

What is step_by method?

By using step by method we can skip every after element for print.And also we can descide whether to print in reverse order or not.

syntax
variable[start index:end index:step_by]
step_by index is also runs by -1.
Lets see example.

Here, in [28] we didn’t put any value in start index:stop index:step_by index,so we got output in clockwise.
But in [29] we put negative step_by and we got anti clockwise which is reverse output,it means step_by index is important for desciding the flow.
in [37] we can see [2:10:2] is means we starts from 2nd element stop on 10th element and skipping after every 1 elements. as we learned earlier step by is always run by -1 element.
same with [36]
[0:8:3] is the order means we are skipping afater every 2 element from 0 to 8 elements for print.

So these are the some basic and simple examples of slicing.We will learn some more exciting things in variables in next article.If you find some useful from this article then like it and show your love and suggestion in comments as well thanks.

#vevcodelab
@sandeshgaikwad

--

--