![]() |
VOOZH | about |
While working with tuples many times we need to access elements at a certain index but for that, we need to know where exactly is that element, and here comes the use of the index() function. In this article, we will learn about the index() function used for tuples in Python.
Example :
The Index() method returns the first occurrence of the given element from the tuple.
Output :
2
Syntax: tuple.index(element, start, end)
Parameters:
- element: The element to be searched.
- start (Optional): The starting index from where the searching is started
- end (Optional): The ending index till where the searching is done
Return type: Integer value denoting the index of the element.
Here we are finding the index of a particular element in a tuple.
Output :
Value of Index of 3 is 1
Here we are finding the index of a particular element in a tuple with multiple occurrences but here it only returns the first occurrence of that element.
Output:
Index of 3 is 0
Here we are finding the index of a particular element in a tuple in the given range.
Output:
Index of G in alphabets from index 4 to 10: 8
Here we are finding the index of a particular element in a tuple that does not exist.
Output:
Traceback (most recent call last):
File "08f51264-102d-4f65-b2d3-692a5a750419.py", line 4, in <module>
print(t.index('i'))
ValueError: tuple.index(x): x not in tuple