Indexing and slicing in Python

In this python tutorial, we will discuss indexing and slicing in Python with a few examples and also we will cover these below topics:

  • What is Indexing in python?
  • Positive indexing example in Python
  • Negative indexing example in Python
  • Indexing in python string
  • Indexing in python list
  • Indexing in python array
  • Indexing in python tuple
  • Indexing in a python dictionary
  • What is slicing in python?
  • Python get substring using slice object
  • Python get substring using negative index
  • Slicing in python list
  • Slicing in python string
  • Slicing in python tuple
  • Slicing in python array

What is Indexing in python?

  • Indexing in Python means referring to an element of an iterable by its position within the iterable.
  • Each character can be accessed using their index number.
  • To access characters in a string we have two ways:
    • Positive index number
    • Negative index number

Positive indexing example in Python

In Python Positive indexing, we pass a positive index that we want to access in square brackets. The index number starts from 0 which denotes the first character of a string.

Example:

my_str = "Python Guides"
print(my_str[0])

In this output, we can see the positive indexing example in python. You can refer to the below screenshot.

Positive indexing example in Python
Positive indexing example in Python

You may like to read, Python program to find sum of n numbers and How to add two numbers in Python.

Negative indexing example in Python

In negative indexing in Python, we pass the negative index which we want to access in square brackets. Here, the index number starts from index number -1 which denotes the last character of a string.

Example:

my_str = "Python Guides"
print(my_str[-1])

In this output, we can see a negative indexing example in python. You can refer to the below screenshot.

Negative indexing example in Python
Negative indexing example in Python

Indexing in python string

String indexing in python allows you to access individual characters from the string directly by using the index. Here, “(str[3])” is used to get “c”.

Example:

str = 'Welcome'
print(str[3])

In this output, we can see indexing in python string. You can refer to the below screenshot.

Indexing in python string
Indexing in python string

You may like Python program to print element in an array.

Indexing in python list

Python List indexing can be done by accessing the list value by referring to its index number. Here, we have used “(list[2])” and it returns “Horse”.

Example:

list = ['Dog', 'Cat', 'Horse', 'Bear']
print(list[2])

In this output, we can see indexing in python list. You can refer to the below screenshot.

Indexing in python list
Indexing in python list

Indexing in Python array

Array indexing in python is the same as accessing an array element. Accessing an array element by referring to its index number. Here, we have used “(my_arr[1])” to access “12”.

Example:

import numpy as np
my_arr = np.array([10, 12, 14, 16])
print(my_arr[1])

In this output, we can see indexing in the python array. You can refer to the below screenshot.

Indexing in python array
Indexing in python array

Check out below Python array tutorials:

Indexing in python tuple

We will use the index operator “[]” to access an item from a Python tuple. Where index starts from 0. Here, we have used a negative index which starts from the right “(my_tuple[-2])” to get “8”.

Example:

my_tuple = (2, 4, 6, 8, 10)
print(my_tuple[-2])

In this output, we can see indexing in python tuple. You can refer to the below screenshot.

Indexing in python tuple
Indexing in python tuple

Indexing in a python dictionary

Indexing a dictionary in Python is to accesses the key or value at that index. Here, we will use list() to index the key of a Python dictionary. To get key at index in the list we have used “m_list[1]” and it will return “John”.

Example:

my_dictionary = {"ani": 101, "John": 102}
m_list = list(my_dictionary)
k = m_list[1]
print(k)

In this output, we can see indexing in a python dictionary. You can refer to the below screenshot.

Indexing in a python dictionary
Indexing in a python dictionary

What is slicing in python?

Slicing in python is used for accessing parts of a sequence. The slice object is used to slice a given sequence or any object. We use slicing when we require a part of a string and not the complete string.

Syntax:

string[start : end : step]

Example:

my_str = "Python Guides"
print(my_str[-1 : -10 : -2])

In this output, we can see what is slicing in python. Here, the step is “-2” so from reverse every other value will be printed. You can refer to the below screenshot.

What is slicing in python?
What is slicing in python

Python get substring using slice object

To get the substring using slice object in python, we will use “slice(5)”. Here, start “0” and the end “5”, so it takes value one less than the end value.

Example:

my_str = 'Guides'
s_obj = slice(5) 
print(my_str[s_obj])

In this output, we can see how to get substring using slice object in python. Here, we can see the substring as “Guide”. You can refer to the below screenshot.

Python get substring using slice object
Python get substring using slice object

Python get substring using negative index

To get substring using negative index in python, we will use “slice(-1, -4, -1)”. Here, the start is “-1”, the end “-4”, and the step is negative “-1”.

Example:

my_str = 'Guides'
s_obj = slice(-1, -4, -1) 
print(my_str[s_obj])

In this output, we can see how to get substring using negative index in python. Here, we can see the substring as “sed”. You can refer to the below screenshot.

Python get substring using negative index
Python get substring using negative index

Slicing in Python list

Lets see slicing in python list.

In this example, we have used “(my_list[-4::1])” for slicing in the list. So, the start is “-4” and we have not specified any value for the end so, by default it will take till last and “1” is step.

Example:

my_list = [10, 20, 30, 40, 50, 60] 
print(my_list[-4::1]) 

In this output, we can see slicing in the python list. Here, we get the list in the output. You can refer to the below screenshot.

Slicing in python list
Slicing in python list

Slicing in python string

Slicing in python string is used for getting a substring from a given string by slicing it. We have specified the start index as “3”, and the end index as “6” and it will return a string.

Example:

str ='Python!'
print(str[3:6])

In this output, we can see slicing in a python string. Here, we get the string as an output. You can refer to the below screenshot.

Slicing in python string
Slicing in python string

Slicing in python tuple

To slice a tuple in Python, we will use “(my_tuple[4])”. To get the value we have to specify a positive integer, to fetch that index from the tuple counting from left. In case of negative index, we have to fetch that index from the right.

Example:

my_tuple = ('v', 'w', 'x', 'y', 'z')
print(my_tuple[4])
print(my_tuple[-3])

In this output, we can see slicing in python tuple. Here, we get the two outputs. You can refer to the below screenshot.

Slicing in python tuple
Slicing in python tuple

Slicing in Python array

To do slicing in the python array, we will import numPy as np. We will define as “(my_arr[1:])”. Here, the start is “1” and the end is not passed so, it will consider till the last length of an array.

Example:

import numpy as np
my_arr = np.array([10, 11, 12, 13, 14])
print(my_arr[1:])

In this output, we can see slicing in the python array. Here, we get the output as an array. You can refer to the below screenshot.

Slicing in python array
Slicing in python array

In this tutorial, we have learned about Indexing and slicing in Python with examples, and also we have covered these topics:

  • What is Indexing in python?
  • Positive indexing example in Python
  • Negative indexing example in Python
  • Indexing in python string
  • Indexing in python list
  • Indexing in python array
  • Indexing in python tuple
  • Indexing in a python dictionary
  • What is slicing in python?
  • Python get substring using slice object
  • Python get substring using negative index
  • Slicing in python list
  • Slicing in python string
  • Slicing in python tuple
  • Slicing in python array