How to Check if a Word is in a String in Python?

In this tutorial, I will explain how to check if a word is in a string in Python. As a Python developer based in the USA, I faced this challenge while developing a text-based application that needed to filter user input based on specific keywords. Let’s get into the various methods you can use to check for the presence of a word in a string, complete with practical examples.

Check if a Word is in a String in Python

Python provides various methods to check if a word is in a string. Let us see some important methods.

Read How to Check if a String is Empty or NaN in Python?

1. Use the in Operator

The simplest and most Pythonic way to check if a word exists in a string is by using the in operator. This operator checks for membership and returns True if the substring is found, and False otherwise.

Example

text = "The quick brown fox jumps over the lazy dog."
word = "fox"

if word in text:
    print("The word is present in the string.")
else:
    print("The word is not present in the string.")

Output:

The word is present in the string.

I have executed the above code and added the screenshot below.

Check if a Word is in a String in Python

This method is not only simple but also very readable, making it a preferred choice among Python developers.

Read How to Check if a String is an Integer or Float in Python?

2. Use the find() Method

Another way to check for a word in a string is by using the find() method. This method returns the lowest index of the substring if found in the string; otherwise, it returns -1.

Example

text = "The United States of America is a great country."
word = "America"

if text.find(word) != -1:
    print("The word is present in the string.")
else:
    print("The word is not present in the string.")

Output:

The word is present in the string.

I have executed the above code and added the screenshot below.

How to Check if a Word is in a String in Python

The find() method is useful when you also need to know the position of the substring within the string.

Read How to Check if a String Starts with a Specific Substring in Python?

3. Use the index() Method

Similar to find(), the index() method can also be used to locate a substring within a string. However, unlike find(), if the substring is not found, index() raises a ValueError.

Example

text = "California is known for its beautiful beaches."
word = "California"

try:
    position = text.index(word)
    print(f"The word is present in the string at index {position}.")
except ValueError:
    print("The word is not present in the string.")

Output:

The word is present in the string at index 0.

I have executed the above code and added the screenshot below.

Check if a Word is in a String in Python index()

This method is beneficial if you want to ensure that the substring exists and handle the case where it does not.

Read How to Check if a String Ends with a Pattern in Python?

4. Use Regular Expressions

For more complex searches, regular expressions (regex) can be very powerful. The re module in Python allows you to perform advanced string searching and manipulation.

Example

import re

text = "The capital of the USA is Washington, D.C."
word = "Washington"

if re.search(word, text):
    print("The word is present in the string.")
else:
    print("The word is not present in the string.")

Using regex is particularly useful when you need to match patterns rather than exact strings. This flexibility makes it a valuable tool for text-processing tasks.

Read How to Check if a String is All Uppercase in Python?

Case Sensitivity

It’s important to note that string searches in Python are case-sensitive. If you need to perform a case-insensitive search, you can convert both the string and the word to the same case using the lower() or upper() methods.

Example

text = "The President of the USA is Joe Biden."
word = "joe"

if word.lower() in text.lower():
    print("The word is present in the string.")
else:
    print("The word is not present in the string.")

In this example, the search will correctly identify “joe” as present in the string, regardless of the case.

ReadHow to Check if a String is a Boolean Value in Python?

Conclusion

In this tutorial, I have explained how to check if a word is in a string in Python. I discussed various methods like using in operator, find() method, index() method, regular expression and case sensitivity.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.