To check if a list of words is in a string using Python, the easiest way is with list comprehension.

list_of_words = ["this","words","string"]

string = "this is a string with words."

print([word in string for word in list_of_words])

#Output:
[True, True, True]

If you want to check if all words of a list are in a string, you can use list comprehension and the Python all() function.

list_of_words = ["this","words","string"]

string = "this is a string with words."

print(all([word in string for word in list_of_words]))

#Output:
True

If you want to check if any of the words of a list are in a string, then you can use the Python any() function.

list_of_words = ["this","words","string"]

string = "this is a string with words."

print(any([word in string for word in list_of_words]))

#Output:
True

When working with strings in Python, the ability to be able to get information about them is valuable.

One such piece of information is if certain words are in a string variable.

You can check if word is a in a string with the Python in operator, but if you want to check if a list of words is in a string, then you have to do a little more.

To check if a list of words is in a string using Python, the easiest way is with list comprehension.

Below are some examples showing you how to use list comprehension to check if more than one word is in a string using Python.

list_of_words = ["this","words","string"]
another_list_of_words = ["a","is","apple"]

string = "this is a string with words."

print([word in string for word in list_of_words])
print([word in string for word in another_list_of_words])

#Output:
[True, True, True]
[True, True, False]

Checking if All Words in List are in String Using Python

If you want to check if all of the words are in a string using Python, you can use the Python all() function.

all() checks if all of the items of an iterable are True.

If all of the items in an iterable is True, then any() returns True.

Below is an example of how to check if all words in a list of words are in a string variable in Python.

list_of_words = ["this","words","string"]

string = "this is a string with words."

print(all([word in string for word in list_of_words]))

#Output:
True

Checking if Any Words in List are in String Using Python

If you want to check if any of the words are in a string using Python, you can use the Python any() function.

any() checks if any of the items of an iterable are True.

If at least one item in an iterable is True, then any() returns True.

Below is an example of how to check if any words in a list of words are in a string variable in Python.

list_of_words = ["this","words","string"]
another_list_of_words = ["a","is","apple"]

string = "this is a string with words."

print(any([word in string for word in list_of_words]))
print(any([word in string for word in another_list_of_words]))

#Output:
True
False

Hopefully this article has been useful for you to learn how to check if a list of words is in a string using Python.

Categorized in:

Python,

Last Update: March 11, 2024