Check if a Word is Present in a Sentence

In Python, checking if a word is present in a sentence is a common task in text processing and string manipulation. Python provides multiple approaches including the in operator, find() method, and split() method for word-by-word matching.

Using the in Operator

The simplest way to check if a word exists in a sentence is using Python's in operator ?

sentence = "The bestseller book is amazing"
word = "book"

if word in sentence:
    print("The given word is present in the sentence.")
else:
    print("The given word is not present in the sentence.")
The given word is present in the sentence.

Note: This method performs substring matching, so it may find partial matches within larger words.

Using the find() Method

The find() method returns the index of the first occurrence of the word, or -1 if not found ?

def word_checker(sentence, word):
    index = sentence.find(word)
    return index != -1

sentence = "Welcome to Python programming"
word = "Python"

if word_checker(sentence, word):
    print(f"'{word}' found at index: {sentence.find(word)}")
else:
    print(f"'{word}' not found in the sentence")
'Python' found at index: 11

Using split() for Exact Word Matching

For exact word matching (avoiding partial matches), split the sentence into words and check each one ?

def exact_word_checker(sentence, word):
    words = sentence.split()
    return word in words

sentence = "Python is a programming language"
word = "Python"

if exact_word_checker(sentence, word):
    print("The exact word is present in the sentence.")
else:
    print("The exact word is not present in the sentence.")

# Test with partial match
sentence2 = "I love Pythonic code"
if exact_word_checker(sentence2, "Python"):
    print("Found exact match")
else:
    print("No exact match found (avoiding 'Pythonic')")
The exact word is present in the sentence.
No exact match found (avoiding 'Pythonic')

Case-Insensitive Word Search

Convert both sentence and word to lowercase for case-insensitive matching ?

def case_insensitive_search(sentence, word):
    return word.lower() in sentence.lower()

sentence = "PYTHON is an Amazing Programming Language"
word = "python"

if case_insensitive_search(sentence, word):
    print("Word found (case-insensitive)")
else:
    print("Word not found")
Word found (case-insensitive)

Comparison of Methods

Method Partial Match Case Sensitive Best For
in operator Yes Yes Quick substring search
find() Yes Yes Getting word position
split() No Yes Exact word matching
lower() + in Yes No Case-insensitive search

Conclusion

Use the in operator for simple substring searches, split() for exact word matching, and combine with lower() for case-insensitive searches. The find() method is useful when you need the position of the word in the sentence.

Updated on: 2026-03-27T12:47:46+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements