index() function in python by R4R Team

-index() is a python function which are used to find the index of element in the given string or list.
-index() take one element as a argument whose index are to be find in the sequence.

Example-
string is "mytext"
index of 't' is 2

list is [1,2,4,3,7,6]
index of 7 is 4

program-

print("Enter first string")
s1=input()
print("Enter any character")
s2=input()
print(s2,"is present in",s1,"at",s1.index(s2),"index")

l=[1,2,3,4]
print("Enter number to find in list")
n=int(input())
print(n,"is present at",l.index(n),"index")


output-

Enter first string
helloworld
Enter any character
w
w is present in helloworld at 5 index
Enter number to find in list [1, 2, 3, 4]
4
4 is present at 3 index

Enter first string
this is the string
Enter any character
the
the is present in this is the string at 8 index
Enter number to find in list [1, 2, 3, 4]
1
1 is present at 0 index


Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!