python输入一个单词和一个句子输出该单词包括其变形在句子中的位置下标从0开始例子:输入 have 和 I had an apple and a banana输出 1
代码如下:
word = input("请输入一个单词:") sentence = input("请输入一个句子:")
将句子按空格拆分为单词列表
word_list = sentence.split(" ")
遍历单词列表,查找目标单词
for i in range(len(word_list)): if word_list[i].startswith(word): print(i) elif word_list[i].endswith(word): print(i) elif word in word_list[i]: print(i)
原文地址: https://www.cveoy.top/t/topic/bb8x 著作权归作者所有。请勿转载和采集!