Python string find, like and contains examples

Python offers several different ways to check if a string contains a substring. IN this article I'll post several different ways:

  • test_string in other_string - return True/False
  • test_word.startswith(word) - return True/False
  • word.find(test_word) - return index

In this post:

Check if string contains substring with in

Contains or like operator in Python can be done by using following statement:

test_string in other_string

This will return true or false depending on the result of the execution. As you can see from the examples below it's case sensitive. There is a way to make is case insensitive by using: mystr.lower()

print('lemon' in 'lemon pie') # True
print('lemon' in 'Lemon') # False
print('LeMoN'.lower() in 'lemon') # True
print('lemon' in 'lemon') # True
print('lemon' in 'Hot lemon pie') # True
print('lemon' in 'orange juice') # False

result:

True
False
True
True
True
False

Check if string contains substring with find

Contains or like operator in Python can be done by using following statement:

sentence = "I want a cup of lemon juice";
test_word = "lemon";
test_word_Up = "LEMON";

print (sentence.find(test_word))
print (sentence.find(test_word_Up))
print (sentence.find(test_word, 5))
print (sentence.find(test_word, 20))

result:

16
-1
16
-1

Test list of strings for a exact match

If you have a list of string with words or sentences you can check them by:

forbidden_list = ['apple juice', 'banana pie', 'orange juice', 'lemon pie', 'lemon']
test_word = 'lemon'
if test_word in forbidden_list:
    print(test_word)

For test_word lemon the result would be:

lemon

For test word 'apple' the result would be empty. This case is useful when you have predefined list of values and you want to verify the tested example is not part of the predefined list with values.

Test list of strings for a substring

If you want to test whether a word is substring of any of the list with words then you can use the following code snippet to check that:

forbidden_list = ['apple juice', 'banana pie', 'orange juice', 'lemon pie', 'lemon']
test_word = 'lemon'
for word in forbidden_list:
    if word.startswith(test_word):
        print(word)

This will search if the test_word is a substring of any of the words in the list. The result would be:

lemon pie
lemon

Test list of strings against another list of values

Often there is a need to filter many messages against list of forbidden values. This can be done by:

  • iterating the list of search words
  • list all forbidden words
  • check if the given word is part of a forbidden word:
forbidden_list = ['apple', 'banana', 'orange', 'lemon', 'kiwi', 'mango']
search_words = ['apple', 'orange', 'lemon']

for test_word in search_words:
    if any(word.startswith(test_word) for word in forbidden_list):
        print(test_word)

using this way you have freedom of choosing what test to be applied - exact match, starting as a string or to be substring:

apple
orange
lemon

For exact match you can try also to use:

diff_list = list(set(forbidden_list) & set(search_words))

['apple', 'orange', 'lemon']

Python string contains or like operator

Below you can find two methods which simulates string contains or like behavior using python:

Testing string against list of string (substring)

If you want to check a given word(s) are they part of a list of a strings this can be done by:

forbidden_list = ['apple', 'banana', 'orange', 'lemon', 'kiwi', 'mango']
forbidden_like_list = ['apple juice', 'banana pie', 'orange juice', 'lemon pie']
search_words = ['apple', 'banana', 'orange', 'lemon']

for test_word in search_words:
    if any(word.startswith(test_word) for word in forbidden_like_list):
        print(test_word)

print('--------------------------------')
test_word = 'lemon'
if any(test_word == word for word in forbidden_list):
    print(word)

result:

apple
banana
orange
lemon


lemon

Python multiple contains - list vs list

This method implements check of a given list if it is part of another list. This can be used as a filter for messages.

forbidden_list = ['apple', 'banana', 'orange', 'lemon', 'kiwi', 'mango']
search_words = ['apple', 'banana', 'orange', 'lemon']

def stringLike(search_words, forbidden_list):
    for line in forbidden_list:
        if any(word in line for word in search_words):
            print(line)

stringLike(search_words, forbidden_list)

result:

apple
banana
orange
lemon

Python like/contains operator - list vs list

Implementation of the this method is similar to the previous one except that check is verifying that string contains another string:

forbidden_like_list = ['apple juice', 'banana pie', 'orange juice', 'lemon pie']
search_words = ['apple', 'banana', 'orange', 'lemon']

def stringIn(search_words, forbidden_like_list):
    for line in forbidden_like_list:
        if any(word in line for word in search_words):
            print(line)

stringIn(search_words, forbidden_like_list)

result:

apple juice
banana pie
orange juice
lemon pie

Related Article