Program to find index of an element in a tuple in Python
In the python programming article, we are going to learn
-
program to find index of an element present in a tuple in python
Program to find index of an element present in a tuple in Python
The program is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
names = ("Devjeet","Ajit","Bulbul","Shawriya")
try:
index_of_devjeet = names.index("Devjeet")
print("The index is:", index_of_devjeet)
except:
print("The element is not present in the tuple.")
The output of the program is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The index is: 0
Few important tips about the program
1. Like in a list, the .index() method returns the index position of an element. The element has to be passed to the method as an argument.
2. We have used exception handling here because, if the element is not present in the tuple, it will raise an exception. The except keyword can handle that exception and print the required statement.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Oct 13,2022