I think what you are trying to achieve is this (since you mentioned you used .split()
in the comments on your question:
sentence = "this is a sentence"
sentence_split = sentence.split()
def wordLength(word, sentence):
try:
index = sentence.index(word)
print(index)
except:
print("word not in sentence")
wordLength("a", sentence_split)
Results in '3', which is the position of 'a' within your sentence.
EDIT
Or, if you want the index number of each letter within each word..
sentence = "this is a sentence"
sentence_split = sentence.split()
letter_index = []
def index_letters():
for i in sentence_split:
# I results in "this" (1st loop), "is" (2nd loop), etc.
for x in range(len(i)):
# loops over every word, and then counts every letter. So within the i='this' loop this will result in four loops (since "this" has 4 letters) in which x = 0 (first loop), 1 (second loop), etc.
letter = i[x]
index = i.index(letter)
letter_index.append([index, letter])
return letter_index
print(index_letters())
Results in: [[0, 't'], [1, 'h'], [2, 'i'], [3, 's'], [0, 'i'], [1, 's'], [0, 'a'], [0, 's'], [1, 'e'], [2, 'n'], [3, 't'], [1, 'e'], [2, 'n'], [6, 'c'], [1, 'e']]