Python tuple Method - index()

The index() method is a built-in tuple method in Python that returns the index of the first occurrence of a specified element in a tuple.

The syntax for using index() method is as follows:

refer to:‮gi‬iftidea.com
tuple.index(element)

Here, tuple is the original tuple, and element is the element whose index needs to be found.

The method returns an integer that represents the index of the first occurrence of the specified element in the tuple.

Here's an example of using the index() method:

tuple1 = (1, 2, 3, 4, 1, 1, 5, 6, 7, 8, 1)
index = tuple1.index(1)
print(index)

Output:

0

In the example above, the index() method was used to find the index of the first occurrence of the element 1 in the tuple (1, 2, 3, 4, 1, 1, 5, 6, 7, 8, 1). The resulting index is stored in the variable index. Note that if the specified element is not found in the tuple, the method raises a ValueError exception. Also note that the method only returns the index of the first occurrence of the specified element in the tuple, and not the indexes of all occurrences.