How do I check for an EXACT word in a string in python

后端 未结 7 2060
再見小時候
再見小時候 2020-12-01 18:27

Basically I need to find a way to figure out a way to find the EXACT word in a string. All the information i have read online has only given me how to search for letters in

7条回答
  •  执笔经年
    2020-12-01 18:48

    Below is a solution without using regular expressions. The program searches for exact word in this case 'CASINO' and prints the sentence.

        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)
    

    This will print the results - ['The Learn Python Challenge Casino.', 'They bought a car while at the casino']

提交回复
热议问题