【发布时间】:2016-05-13 03:23:35
【问题描述】:
这是我的代码:
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)
我能够获取字符串中单词第一次出现的索引。我怎样才能获得所有的出现?
【问题讨论】:
-
纯代码编写请求在 Stack Overflow 上是题外话——我们希望这里的问题与特定编程问题有关——但我们很乐意帮助您自己编写!告诉我们what you've tried,以及您遇到的问题。这也将有助于我们更好地回答您的问题。
-
@GeorgeStocker:虽然我能理解它不应该被删除,但这是一个代码请求的教科书案例......
-
@Cerbrus 我还没有找到一个密切的理由说明“纯代码编写请求”是题外话;事实上,我们明确表示同意他们,只要他们不要求太多:meta.stackexchange.com/a/224104/16587
-
@GeorgeStocker 作为骗子关闭它怎么样? Find all occurrences of a substring in Python。我会等到得到你的同意后再敲门。
标签: python string indexing find-occurrences