【发布时间】:2017-01-27 16:41:00
【问题描述】:
Python 3.5
这是我的代码:
str1 = input("Please enter a full sentence: ").lower()
print("Thank you, You entered:" , str1)
str2 = input("Now please enter a word included in your sentence in anyway you like: ").lower()
if str2 in str1:
print("That word was found!")
else:
print("Sorry, that word was not found")
按原样,它将搜索输入的单词(str2),如果在输入(str1(一个句子))中找到它,它会说“该单词已找到”)。如果该单词不在句子中,它将显示“未找到该单词”。
我想开发这个,所以当搜索并找到单词时,它会告诉用户单词 (str2) 在句子 (str1) 中的索引位置。例如:如果我有句子(“I like to code in Python”)并且我搜索单词(“code”),程序应该说“该单词在索引位置找到:4”。
顺便说一句,代码不区分大小写,因为它使用 .lower 将所有单词转换为小写。
如果有人可以给我一些建议,那将非常感激!
【问题讨论】:
-
您使用的是 Python 2.7 还是 Python 3?
标签: string indexing python-3.5