Skip to content
Home / Fundamentals

Checking if a String Contains a Substring in Python

Method 1: in keyword

In Python, you can use the in keyword to check if a string contains a substring. Here is the syntax for using the in keyword:

substring in string

The in keyword returns a boolean value: True if the substring is found in the string, and False if it is not.

Here are some examples of using the in keyword to check if a string contains a substring:

# Check if the string "cat" is in the string "cat in the hat"
print("cat" in "cat in the hat")  # Output: True

# Check if the string "dog" is in the string "cat in the hat"
print("dog" in "cat in the hat")  # Output: False

# Check if the string "in" is in the string "cat in the hat"
print("in" in "cat in the hat")  # Output: True

# Check if the string "cat" is in the string "The cat in the hat" (case-sensitive)
print("cat" in "The cat in the hat")  # Output: True

# Check if the string "Cat" is in the string "The cat in the hat" (case-sensitive)
print("Cat" in "The cat in the hat")  # Output: False

Note that the in keyword is case-sensitive, so "cat" and "Cat" are considered to be different substrings.

You can also use the in keyword to check if a substring is contained in a list of strings. For example:

# Check if the string "cat" is in the list ["cat", "dog", "bird"]
print("cat" in ["cat", "dog", "bird"])  # Output: True

# Check if the string "lion" is in the list ["cat", "dog", "bird"]
print("lion" in ["cat", "dog", "bird"])  # Output: False
If you need to perform a case-insensitive check for a substring in a string or list, you can use the lower() or upper() method to convert the string and the substring to all lowercase or all uppercase, respectively, before using the in keyword. For example:

```python
# Check if the string "Cat" is in the string "The cat in the hat" (case-insensitive)
print("Cat" in "The cat in the hat".lower())  # Output: True

# Check if the string "lion" is in the list ["Cat", "Dog", "Bird"] (case-insensitive)
print("lion" in [s.upper() for s in ["Cat", "Dog", "Bird"]])  # Output: False

Method 2: find() method

Another option is to use the find() method of strings, which returns the index of the first occurrence of a substring in a string. If the substring is not found, the find() method returns -1. Here is an example of using the find() method to check if a string contains a substring:

string = "cat in the hat"

# Check if the string "cat" is in the string "cat in the hat" using the find() method
if string.find("cat") != -1:
    print("The string 'cat' was found in the string '{}'".format(string))
else:
    print("The string 'cat' was not found in the string '{}'".format(string))

# Check if the string "dog" is in the string "cat in the hat" using the find() method
if string.find("dog") != -1:
    print("The string 'dog' was found in the string '{}'".format(string))
else:
    print("The string 'dog' was not found in the string '{}'".format(string))

# Check if the string "in" is in the string "cat in the hat" using the find() method
if string.find("in") != -1:
    print("The string 'in' was found in the string '{}'".format(string))
else:
    print("The string 'in' was not found in the string '{}'".format(string))

# Check if the string "cat" is in the string "The cat in the hat" (case-sensitive) using the find() method
if string.find("cat") != -1:
    print("The string 'cat' was found in the string '{}'".format(string))
else:
    print("The string 'cat' was not found in the string '{}'".format(string))

# Check if the string "Cat" is in the string "The cat in the hat" (case-sensitive) using the find() method
if string.find("Cat") != -1:
    print("The string 'Cat' was found in the string '{}'".format(string))
else:
    print("The string 'Cat' was not found in the string '{}'".format(string))

Note that the find() method is also case-sensitive, so "cat" and "Cat" are considered to be different substrings.

If you need to perform a case-insensitive check for a substring in a string using the find() method, you can use the lower() or upper() method to convert the original string

# Check if the string "Cat" is in the string "The cat in the hat" (case-insensitive) using the find() method
string = "The cat in the hat"
if string.lower().find("Cat".lower()) != -1:
    print("The string 'Cat' was found in the string '{}'".format(string))
else:
    print("The string 'Cat' was not found in the string '{}'".format(string))

Method 3: startswith() method

Alternatively, you can use the startswith() method to check if a string starts with a particular substring. Here is an example of using the startswith() method:

# Check if the string "cat" is at the beginning of the string "cat in the hat"
print("cat in the hat".startswith("cat"))  # Output: True

# Check if the string "in" is at the beginning of the string "cat in the hat"
print("cat in the hat".startswith("in"))  # Output: False

# Check if the string "Cat" is at the beginning of the string "The cat in the hat" (case-sensitive)
print("The cat in the hat".startswith("Cat"))  # Output: False

# Check if the string "Cat" is at the beginning of the string "The cat in the hat" (case-insensitive)
print("The cat in the hat".lower().startswith("Cat".lower()))  # Output: True