Check if string appears as its own word - Python

前端 未结 4 463
生来不讨喜
生来不讨喜 2021-01-23 15:55

Let\'s say that I am looking for the word \"or\". What I want is to check whether that word appears as a word or as a substring of another word.

E.g.

<
4条回答
  •  悲&欢浪女
    2021-01-23 16:33

    Create a checker with just a test if it is in the line

    def check_word_in_line(word, line):
        return " {} ".format(word) in line
    
    print(check_word_in_line("or", "I can go shopping or not")) //True
    print(check_word_in_line("or", "I can go shopping for shoes")) //False
    

提交回复
热议问题