What is Indexing in Python?

What is indexing in python: In Python Indexing is the process of extracting individual elements from a given collection.

Whenever there is a data stored in memory they have certain address where they stored and they have some positions upon memory called index positions. Generally the positions starts with 0 up to total number of elements minus 1. Simply 0 to length( variable-1) .

Let’s understand with an example

suppose the above is a string “hello python”, which we are storing in a variable called as str=”hello python”

So the length of this variable is 12 ( including space) and its index position started from 0 & up to 11.

So if we need to extract any element from it we need to mentions its index position.

Now if need to extract character p, we need to write code like below…

str='hello python'
str[6]

#will get output like this
'p'

What is Indexing in Python in Hindi video

Types of Indexing in Python

There are two types of index positions

  • Positive Index Position
  • Negative Index Position

Positive Indexing in Python

Positive indexing in python

In positive indexing system, positive index positions get used where direction of reading elements will be left to right and the count will be start from 0.

From the above string , if I access ‘t’ I have write code like this

str='hello python'
str[8]

't' #Output

Negative Indexing in Python

In negative indexing system, negative index positions get used where direction of reading elements will be right to left and the count will be start from -1 and up to -(length of data or variable).

From the above string , if I need to access ‘t‘ i have to write code like below code

str='hello python'
str[-4]

't' #Output

Leave a Comment