在字符串中查找 word/substring 的位置 (python 3.4.3)

Find position of word/substring in string (python 3.4.3)

我想找到 WHOLE 单词在字符串中的位置,但我的代码显示的是单词第一个字符的位置。

string = input("Please input a sentence: ")
word = input("Please input a word: ")
string.lower()
word.lower()
list1 = string.split(' ')
position = string.index(word)
print (position)

既然你要求单词作为输入,你有单词大小并且你知道起始索引,你可以这样做:起始索引 + 大小。

string = input("Please input a sentence: ")
word = input("Please input a word: ")
string.lower()
word.lower()
list1 = string.split(' ')
position = string.index(word)
print (position) // starting position
print (position + len(word) ) // end position