
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Test if string contains element from list
In this article, we will learn how to check if a string contains an element from a list in python.
Methods Used
Using Nested For Loops
Using List Comprehension
Using any() function
Using the find() function
Example
Assume we have taken an input string and input list. We will now check whether the input string contains at least any one input list element.
Input
inputString = "tutorialspoint is a best learning platform for coding" inputList = ['hello', 'tutorialspoint', 'python']
Output
YES, the string contains elements from the input list
In the above example, the input string contains 'tutorialspoint' so yes is the answer.
Method 1: Using Nested For Loops
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input string.
Create another variable to store the input list.
Use the split() function(splits a string into a list. We can define the separator; the default separator is any whitespace) to split the input string into a list of words.
Initialize a temporary flag(temp_flag) variable with 0.
Use the for loop to traverse through the above-split words list.
Use another nested for loop to traverse through the input list
Use if conditional statement to check whether both elements are equal.
Set the temp_flag with 1 if the condition is true.
Use the break statement to break the loop if the temp_flag becomes 1.
Use the if conditional statement to check whether the value of temp_flag is 1.
Print the result
Example
The following program checks whether the string contains any input list element using for nested for loops –
# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['hello', 'tutorialspoint', 'python'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) # splitting the input string into a list of words wordsList = inputString.split(" ") # temporary flag variable temp_flag = 0 # traversing through the above-split words list for p in wordsList: # traversing through the input list for q in inputList: # checking whether both the elements are equal if p == q: # Set the value of temp_flag by 1 if the condition is true temp_flag = 1 # breaking from the loop if the temp_flag becomes 1 break # checking whether the value of temp_flag is 1 if temp_flag == 1: # printing "YES” if the condition is true print("YES, the string contains elements from the input list") else: # else print "NO" print("NO, the string does not contain elements from the input list")
Output
On execution, the above program will generate the following output –
Input string: tutorialspoint is a best learning platform for coding Input List: ['hello', 'tutorialspoint', 'python'] YES, the string contains elements from the input list
Method 2: Using List Comprehension
List Comprehension
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
bool() function − returns the boolean value of a given object
Example
The following program checks whether the input string contains any input list element using list comprehension –
# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['hello', 'tutorialspoint', 'python'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # checking whether the input string contains the list element # using list comprehension output = [i for i in inputList if(i in inputString)] # printing the resulting output as boolean print("Checking whether input string contains the list element:", bool(output))
Output
Input string: tutorialspoint is a best learning platform for coding Input List: ['hello', 'tutorialspoint', 'python'] Checking whether input string contains the list element: True
Method 3: Using any() function
The any() function returns True if any of the items in an iterable are true, else returns False.
Syntax
any(iterable)
Example
The following program checks whether the input string contains any input list element using any() function –
# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['bat', 'cat', 'dog'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # checking whether the input string contains the list element # using any() function output = any(k in inputString for k in inputList) print("Checking whether input string contains the list element:", bool(output))
Output
Input string: tutorialspoint is a best learning platform for coding Input List: ['bat', 'cat', 'dog'] Checking whether input string contains the list element: False
Method 4: Using the find() function
In this method, we are using the find() method to see if the word is present in the list; it returns -1 otherwise.
find() method
Finds the first occurrence of the given value. It returns -1 if the value is not found.
Syntax
string.find(value, start, end)
Example
The following program checks whether the input string contains any input list element using the find() function –
# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['bat', 'cat', 'dog'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # Assuming the result as False initially reslt = False # intilializig a variable with 0 count = 0 # travsering through the input list for item in inputList: # checking whether the current list item is found in the string if(inputString.find(item) != -1): # incrementing the count value by 1 if the condition is true count += 1 # checking whether the count value is greater than or equal to 1 if(count >= 1): # assign result as True if the condition is true reslt = True print("Checking whether input string contains the list element:", bool(reslt))
Output
Input string: tutorialspoint is a best learning platform for coding Input List: ['bat', 'cat', 'dog'] Checking whether input string contains the list element: False
Conclusion
In this article, we learned how to use four different methods to determine whether a string contains an element from a list. Additionally, we learned how to display the result as a boolean rather than using conditional statements.
- Related Articles
- Python Program to Test if any set element exists in List
- Python – Test if tuple list has a single element
- Test if Tuple contains K in Python
- How to test if a string contains specific words in Java?
- How to test if an element contains class in JavaScript in HTML?
- Python program to check if a string contains all unique characters
- Python program to check if a string contains any unique character
- Python program to test if all y occur after x in List
- Python – Test if list is Palindrome
- Program to check if a string contains any special character in Python
- Python Program to check if String contains only Defined Characters using Regex
- How to check if Java list contains an element or not?
- Python – Test if elements of list are in Min/Max range from other list
- How to test if a Java String contains a case insensitive regex pattern
- Python Program to print element with maximum vowels from a List
