的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)中的索引位置。例如:如果我有句子(“我喜欢用Python编码”),并搜索单词(“代码”),程序应该说“该单词在索引位置:4”。
顺便说一句,代码不区分大小写,因为它将所有的单词转换成小写的.lower。
如果有人可以给我一些建议,那么这将非常感激!
你使用Python 2.7还是Python 3? – PrestonM