下面的代碼顯示「字」的位置,如果它在字符串中出現一次。如何更改我的代碼,以便如果「字」在字符串中出現多次,它將打印所有位置?如何在字符串中找到多於一個子字符串的位置(Python 3.4.3 shell)
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))
*您 –
實際上它是在列表中的位置,因爲你是用文字 – tinySandy