我正在编写一个Python程序,可以在其中获取单词在段落中的位置以及单词在程序中包含的段落中出现的次数。目前,我的代码仅输出输入的单词的2个位置。
text = '''This is sample text to test if this pythonic program can
serve as
an indexing platform for finding words in a paragraph. It can give
values
as to where the word is located with the different examples as
stated'''
find_str = input("Please Enter Word to Search: ")
x = 0
i = text.find(find_str, x)
print (i)
i = text.find(find_str, i+1)
print (i)
如果我输入“ as”,则输出为:
Please Enter Word to Search: as
63
140
>>>
如何使代码输出所输入单词的所有位置,以及单词在段落中出现的次数?