小编Mr.*_*ode的帖子

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

这是我的代码:

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)
Run Code Online (Sandbox Code Playgroud)

我能够获得字符串中第一次出现的单词的索引.我怎样才能得到所有的事件?

python string indexing find-occurrences

-64
推荐指数
1
解决办法
2万
查看次数

标签 统计

find-occurrences ×1

indexing ×1

python ×1

string ×1