下面是一个不使用正则表达式的解决方案。程序在这种情况下搜索确切的单词“CASINO”并打印该句子。
words_list = [ "The Learn Python Challenge Casino.", "They bought a car while at
the casino", "Casinoville" ]
search_string = 'CASINO'
def text_manipulation(words_list, search_string):
search_result = []
for sentence in words_list:
words = sentence.replace('.', '').replace(',', '').split(' ')
[search_result.append(sentence) for w in words if w.upper() ==
search_string]
print(search_result)
text_manipulation(words_list, search_string)
这将打印结果 - ['The Learn Python Challenge Casino.', '他们在赌场买了一辆车']