繁体   English   中英

查找单词/子字符串在字符串中的位置(python 3.4.3)

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

我想查找的字符串在整个字的位置,但我的代码显示该单词的第一个字符的位置。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM