【发布时间】:2016-02-02 20:33:59
【问题描述】:
以下代码显示“word”在字符串中出现一次时的位置。如何更改我的代码,以便如果“单词”在字符串中出现多次,它将打印所有位置?
string = input("Please input a sentence: ")
word = input("Please input a word: ")
string.lower()
word.lower()
list1 = string.split(' ')
position = list1.index(word)
location = (position+1)
print("You're word, {0}, is in position {1}".format (word, location))
【问题讨论】:
-
实际上它在列表中的位置,因为您将字符串按单词拆分为列表
标签: python string python-3.x indexing position