Dive into INDEXING vs SLICING in Python

Image Source: Author

Most of us must be wondering why I have come up with this. I guess I am right. Ain’t I? While doing the project I got stuck on a couple of pretty basic use cases. So after poking around a bit to get un-stuck, I figured it was worth sharing what I learned.

Definition :

Slicing: Slicing is a technique that takes elements from one given index to another given index.

INDEXING

Indexing in String
Indexing in String
Indexing in String
Indexing in List

SLICING

Image Source: Author

I know that here you must have got confused, just have a look at the examples given below:

Few Special feature of slicing :

[:] refers to complete duplication of the given sequence

[m:] refers to the element set from the start position i.e m here to the end of the given sequence.

[:n] refers to the element set from the start of the given sequence to the given end position i.e n.

STEP in Slicing: Integer value which determines the increment between each index for slicing. Defaults to None if not provided.

Hope the article helps you out and clears your basic concepts.

Thank You !!

3rd year CSE student at IIITKALYANI , enthusiastic learner and explorer