Python 如何查找字符串中出现的所有单词的所有索引

Python 如何查找字符串中出现的所有单词的所有索引,python,string,indexing,find-occurrences,Python,String,Indexing,Find Occurrences,这是我的代码: sentence = input("Give me a sentence ") word = input("What word would you like to find ") sentence_split = sentence.split() if word in sentence_split: print("have found",word,) print("The word comes in the position" ) else: prin

这是我的代码:

sentence = input("Give me a sentence ")

word = input("What word would you like to find ")


sentence_split = sentence.split()


if word in sentence_split:
   print("have found",word,)
   print("The word comes in the position" )
else:
   print("error have not found",word)

wordfound = (sentence_split.index(word)+1)

print(wordfound)
我能够得到字符串中第一个出现的单词的索引。如何获取所有发生的事件?

使用:

对于
word=“this”
station=“this is a句子this”
这将产生输出:

(0, 4)
(19, 23)
(24, 28)

纯代码编写请求与堆栈溢出无关——我们希望这里的问题与特定的编程问题有关——但我们很乐意帮助您自己编写!告诉我们,你被困在哪里了。这也有助于我们更好地回答您的问题。@GeorgeStocker:虽然我能理解它不应该被删除,但这是一个关于代码请求的教科书案例…@Cerbrus我还没有找到一个明确的理由说明“纯代码编写请求”是离题的;事实上,只要他们不要求太多,我们可以明确地接受他们:@GeorgeStocker作为一个傻瓜来结束它怎么样。我会等你批准后再敲打。我认为值得指出的是,它只适用于“非重叠匹配”,因此不适用于:句子=“aba”和word=“aba”
(0, 4)
(19, 23)
(24, 28)