index of substring in a string python code example
Example 1: python string index of
sentence = 'Python programming is fun.'
result = sentence.index('is fun')
print("Substring 'is fun':", result)
result = sentence.index('Java')
print("Substring 'Java':", result)
Example 2: python get index of substring in liast
def index_containing_substring(the_list, substring):
for i, s in enumerate(the_list):
if substring in s:
return i
return -1
Example 3: python get index of substring in liast
index = [idx for idx, s in enumerate(l) if 'tiger' in s][0]