How to Use Tuple index() Method in PythonPython Basics


The tuple.index() method searches an element in a tuple.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

temp_tuple = ('a', 'b', 'v', '1', '5', '2', '4', '9', '8','w','z','h')
index=temp_tuple.index('a')
print('the index of a is: ',index)

index=temp_tuple.index('5')
print('the index of 5 is: ',index)

index=temp_tuple.index('v')
print('the index of v is: ',index)

Output:

the index of a is:  0
the index of 5 is:  4
the index of v is:  2

Syntax

tuple.index(x)

Parameters

Name Description
x The item that is to be searched.

Return Value

It returns the index of the given element in the tuple.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us