my_string = "Hello World!"
my_string.index("l") # outputs 2
# this method only outputs the index of the first "l" value in the string/list
Python Program to Find the index of element in the list
# Programming list
programming_list = ['C','C#','Python','Java','JavaScript','SQL']
# find the index of Python
index = programming_list.index('Python')
print('The index of Python is :', index)
# find the index of SQL
index = programming_list.index('SQL')
print('The index of SQL is :', index)
1) how to find index of a word in a string:
my_string = "Hello World!"
my_string.index("l")
2) how to find the first occurance of a word in a string:
print(verse.index('and'))
3)how to find the last occurance of a word in a string:
print(verse.rindex('you'))
#index with eval()
k=eval(input("enter the list"))
for i in range(len(k)):
print(k[i],"-->forward index",i,"backward index",-(len(k)-i))
#output
enter the list[1,9,5,7]
1 --> forward index 0 backward index -4
9 --> forward index 1 backward index -3
5 --> forward index 2 backward index -2
7 --> forward index 3 backward index -1
ValueError: 'item' is not in list
# Programming list
programming_list = ['C','C#','Python','Java','JavaScript','SQL','Java','F#','Go']
# find the lowest index of HTML
index = programming_list.index('HTML')
print('The index of HTML is :', index)
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
TestString = "Python Is Fun"
print(TestString[2])
#This will print "t"
#printing it backwards would be
print(TestString[::-1])
#these an be stored in variables too.
a = TestString[0:5]
print(a)