Find a word in a sentence in Python

Find a word in a sentence in Python. Sometimes we have to search for a specific word in a sentence while text processing. This find_word function will help you to find a specific word in a sentence.

Source code :


def find_word(word, sentence):
    
    search = sentence.split()
    
    for search_word in search:
        if search_word == word:
            return True
            
    return False
	
	

print(find_word("Hello","Hello world!"))
#'Hello' is in the sentence hence it returns True

print(find_word("John","Johnny is a good boy"))
#'John' is not in the sentence hence it returns False

print(find_word("Jackma","His name is Jackma"))
#'Jackma' is  in the sentence hence it returns True

Output :

True
False
True

Notes :

The first parameter is the word to find, the second parameter is the sentence.

Here is the logic. Initially we will split the sentence and iterate into it,
if the give word matches the one in the sentence it will return True, or else it will return False by default.


Output :


                

Comments :