Python Indexing Operation on String, List, and Tuple

In python, we have different container objects like lists, tuples, and strings that store elements in a sequence. We can access the elements of these objects using the python indexing operator. In this article, we will discuss what an indexing operator is and how we can use it in Python.

What Is the Indexing Operator in Python?

The square brackets [] are called indexing operators in Python. The indexing operator selects one or more elements from a container object like a list, python string, or tuple. It has the following syntax.

python_obj[start_index:end_index:interval]

Here,

  • python_obj is a python object such as a python list, string, or tuple.
  • start_index is the index of the first element in python_obj that we want to extract.
  • end_index is the index of the last element in python_obj that we want to extract. Here, end_index is exclusive. Hence, the element at end_index is not included in the output.  Instead, the elements till the index end_index-1 are included in the output.
  • By default, the indexing operator selects all the elements from start_index to end_index-1 in python_obj. You can use the interval parameter if you want to select only specific elements from the python object using the indexing operator. 
  • The interval parameter takes the index interval of elements in python_obj that we want to select. For instance, If we specify interval as a number n, the indexing operator selects elements from python_obj at the index start_index,start_index+n, start_index+2n, start_index+3n, and so on.

String Indexing in Python

String indexing in python is used to select specific characters from a string. To select a single character from a string, you can use the index of the character with the indexing operator []  as shown in the following example.

myStr="Avid Python"
print("The string is:",myStr)
index=6
character=myStr[index]
print("The character at index 6 is:",character)

Output:

The string is: Avid Python
The character at index 6 is: y

In the above example, we have taken a python string "Avid Python". When we pass index 6 to the indexing operator, we get the fifth character of the string.

If you want to select a range of characters from the string using the indexing operator, you can use the start_index and the end_index parameter in the indexing operator as shown below.

myStr="Avid Python"
print("The string is:",myStr)
start_index=5
end_index=9
characters=myStr[start_index:end_index]
print("The characters at index 5 to 9 are:",characters)

Output:

The string is: Avid Python
The characters at index 5 to 9 are: Pyth

In this example, we have selected characters from index 5 to 9 in the string "Avid Python". You can observe that the output contains characters only till index 8. Hence, the index given to the end_index parameter as input is excluded from the output string.

If you want to skip certain characters within the range start_index to end_index, you can use the interval parameter in the indexing operator as shown below.

myStr="Avid Python"
print("The string is:",myStr)
start_index=2
end_index=9
interval=2
characters=myStr[start_index:end_index:interval]
print("The characters at index 2 to 9 with interval 2 are:",characters)

Output:

The string is: Avid Python
The characters at index 2 to 9 with interval 2 are: i yh

In this example, we have selected characters from index 2 to 9. However, we have taken an interval of 2 between the indices of the characters that are selected from the string "Avid Python".

In the above examples, you can observe that using the indexing operator on a string always gives a string as output.

List Indexing in Python

List indexing follows the same syntax as string indexing. List indexing is used to select specific elements of a list. To select a single element from a given python list, we can use the index of the element as shown below.

myList=[23,12,222,121,44,55,32,43]
print("The list is:",myList)
index=4
element=myList[index]
print("The element at index 4 is:",element)

Output:

The list is: [23, 12, 222, 121, 44, 55, 32, 43]
The element at index 4 is: 44

In the above example, we have created a list named myList. Then, we selected the element at index 4 of myList using the indexing operator.

If you want to select specific elements from a given index to another, you can use the strart_index and end_index parameters. If you have to select elements from a given start_index till the end_index, you can use list indexing as shown below.

myList=[23,12,222,121,44,55,32,43]
print("The list is:",myList)
start_index=3
end_index=7
elements=myList[start_index:end_index]
print("The elements at index 3 to 7 are:",elements)

Output:

The list is: [23, 12, 222, 121, 44, 55, 32, 43]
The elements at index 3 to 7 are: [121, 44, 55, 32]

Here, we have selected elements from index 3 to 7 from myList. Again, you can observe that the element at index 7 is excluded from the output.

You can also skip elements between start_index and end_index. For instance, if you want to select elements at a specified interval, you can use the interval parameter in the indexing operator as shown below.

myList=[23,12,222,121,44,55,32,43]
print("The list is:",myList)
start_index=3
end_index=7
interval=2
elements=myList[start_index:end_index:interval]
print("The elements at index 3 to 7 with interval 2 are:",elements)

Output:

The list is: [23, 12, 222, 121, 44, 55, 32, 43]
The elements at index 3 to 7 with interval 2 are: [121, 55]

In this example, we have selected elements from index 3 to 7 from myList. However, we have introduced an interval of 2 between the indices of elements from myList that are selected in the output.

When we use the indexing operator on a python list, we always get a list as output.

Tuple Indexing in Python 

Tuple indexing is exactly the same as list indexing. You can select a specific element from a python tuple using the index of the element and the indexing operator as shown below.

myTuple=(12, 23, 32, 43, 44, 55, 121)
print("The tuple is:",myTuple)
index=1
element=myTuple[index]
print("The element at 1 is:",element)

Output:

The tuple is: (12, 23, 32, 43, 44, 55, 121)
The element at 1 is: 23

In the above example, we have created a tuple named myTuple. Then, we selected the element at index 1 in myTuple using the python indexing operator.

You can also select elements within a range using the indexing operator, the start_index parameter, and the end_index parameter as shown below.

myTuple=(12, 23, 32, 43, 44, 55, 121)
print("The tuple is:",myTuple)
start_index=1
end_index=6
elements=myTuple[start_index:end_index]
print("The elements at index 1 to 6 are:",elements)

Output:

The tuple is: (12, 23, 32, 43, 44, 55, 121)
The elements at index 1 to 6 are: (23, 32, 43, 44, 55)

Here, we have selected elements from index 1 to 6 in myTuple. Again, the element at index 6 is excluded from the output.

If you want to skip certain elements in the range start_index to end_index, you can use the interval parameter as shown below.

myTuple=(12, 23, 32, 43, 44, 55, 121)
print("The tuple is:",myTuple)
start_index=1
end_index=6
interval=2
elements=myTuple[start_index:end_index:interval]
print("The elements at index 1 to 6 with interval 2 are:",elements)

Output:

The tuple is: (12, 23, 32, 43, 44, 55, 121)
The elements at index 1 to 6 with interval 2 are: (23, 43, 55)

In the above examples, you can observe that using the indexing operator on a tuple always gives a tuple as output.

Negative Indexing in Python

Normally, we use numbers starting from 0 to select elements from a list or string using the indexing operator. This allows us to select elements from the start of the string or list. However, we can also use negative indexing in Python to select elements from the end of a list or string.

For example, if you want to select Nth element from the end of a list, you can pass -N to the indexing operator as shown below.

myList=[23,12,222,121,44,55,32,43]
print("The list is:",myList)
N=-4
element=myList[N]
print("The element at index -4 is:",element)

Output:

The list is: [23, 12, 222, 121, 44, 55, 32, 43]
The element at index -4 is: 44

In the above example, we have passed index -4 to the indexing operator. Hence, we get the 4th element from the end of the list as output.

In a similar manner, you can select the Nth character from the last of a string as shown below.

myStr="Avid Python"
print("The string is:",myStr)
N=-4
character=myStr[N]
print("The character at index -4 is:",character)

Output:

The string is: Avid Python
The character at index -4 is: t

You can also select the Nth element from the end of a tuple as shown in the following example.

myTuple=(12, 23, 32, 43, 44, 55, 121)
print("The tuple is:",myTuple)
N=-4
element=myTuple[N]
print("The element at index -4 is:",element)

Output:

The tuple is: (12, 23, 32, 43, 44, 55, 121)
The element at index -4 is: 43

Conclusion

In this article, we have discussed the indexing operator in Python. We also discussed list indexing, string indexing, and tuple indexing with examples. 

I hope you enjoyed reading this article. To learn more about python programming, you can read this article on try-except vs if else in Python. You might also like this article on command line arguments using sys.argv list in python

Donate to Avid Python

Dear reader,
If you found this article helpful and informative, I would greatly appreciate your support in keeping this blog running by making a donation. Your contributions help us continue creating valuable content for you and others who come across my blog. 
No matter how big or small, every donation is a way of saying "thank you" and shows that you value the time and effort we put into writing these articles. If you feel that our article has provided value to you, We would be grateful for any amount you choose to donate. 
Thank you for your support! 
Best regards, 
Aditya
Founder

If you want to Pay Using UPI, you can also scan the following QR code.

Payment QR Code
Payment QR Code

Leave a Reply