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/Falsetest_word.startswith(word)
- return True/Falseword.find(test_word)
- return index
In this post:
- Check if string contains substring with in
- Check if string contains substring with find
- Test list of strings for a exact match
- Test list of strings against another list of values
- Python string contains or like operator
Like operator in Python - in
Python contains or like operator in Python can be done by using operator - in
:
test_string in other_string
This will return True
or False
depending on the result of the execution.
As we 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
Python string - find()
Like operator in Python or string contains - we can check if a string contains a substring by using method .find()
:
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
Check list of strings - exact match
If you have a list of strings 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)
Check if any of the words in this list contains word lemon
:
lemon
Testing with the word 'apple' the result is empty.
This case is useful when you have a predefined list of values and you want to verify the tested example is not part of the predefined list with values.
Python find string in list
In this section we will test whether a string is substring of any of the list items. To do so we are going to loop over each string and test it by in
, find()
or .startswith()
:
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 check if the word is a substring of any of the strings in the list.
The result is:
lemon pie
lemon
Compare two lists of strings
Often there is a need to filter many strings against a 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 like function
This method implements a 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 string_like(search_words, forbidden_list):
for line in forbidden_list:
if any(word in line for word in search_words):
print(line)
string_like(search_words, forbidden_list)
result:
apple
banana
orange
lemon
Python like/contains operator
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 string_in(search_words, forbidden_like_list):
for line in forbidden_like_list:
if any(word in line for word in search_words):
print(line)
string_in(search_words, forbidden_like_list)
result:
apple juice
banana pie
orange juice
lemon pie