If you don't need to know exactly which element(s) contain the substring, then you could use the any built-in function in combination with a list comprehension l = ["Hey dude", "Hey bro", "Sup dude", "Hey bud"] substring = "Sup" if any([substring in element for element in l]): print(f"One or more elements of {l} contain the substring {substring}") else: print(f"No elements of {l} contain the substring {substring}") Here's an explanation about the any function. Alternatively, switch out any for all if you want to check if all the elements of the list contain the substring. all documentation Answer from _MuchUsername_ on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to check if any element in a list contains a substring
r/learnpython on Reddit: How to check if any element in a list contains a substring
October 28, 2019 -

Say I have a list of strings like: ["Hey dude", "Hey bro", "Sup dude", "Hey bud"]

How do I check if *any* of those strings in the list contain the word "Sup"?Alternatively, in my case, I could also check if *all* of the strings contain "Hey" , which I also don't know how to do.

(Sorry, I'm new to Python)

Edit: Thank you so much for all the help, guys <3

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-test-if-string-contains-element-from-list
Python - Test if string contains element from list - GeeksforGeeks
July 11, 2025 - Let's explore some more methods to check how we can test if string contains elements from a list. ... This approach explicitly iterates through the list using a for loop to check for the presence of elements in the string. ... s = "Python is powerful and versatile." el = ["powerful", "versatile", "fast"] # Initialize the result variable to False res = False # Iterate through each element in the list for elem in el: if elem in s: res = True break print(res)
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-check-if-string-contains-substring-from-list
Python - Check if String contains Substring from List
source_string = 'a b c d e f' list_of_strings = ['k', 'm', 'e' ] for substring in list_of_strings: if substring in source_string: print('String contains substring from list.') break
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i check if string contains all substrings in list?
r/learnpython on Reddit: How do I check if string contains all substrings in list?
November 16, 2018 -

I have a list, that takes inputs from the user, this means it can change size; for example: list = ["test", "do"] or list = ["a", "b", "c"]

I want to check if variable string, contains all the substrings in list.

I thought if I used:

if list in string: that would work, but it didn't, TypeError: 'in <string>' requires string as left operand, not list

Is there another function/method for this? Or am I going to have to loop len(list) times (I really don't want to do that, because I check hundreds of changing strings)?

๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-check-if-string-contains-element-from-list
Check if a String contains an Element from a List in Python | bobbyhadz
You can use the assignment expression syntax if you need to get the list item that is contained in the string. ... Copied!my_str = 'one two three' my_list = ['a', 'two', 'c'] if any((match := substring) in my_str for substring in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one element from the list') print(match) # ๐Ÿ‘‰๏ธ 'two' else: print('The string does NOT contain any of the elements in the list')
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ check-if-string-contains-substring-in-python
Check if String Contains Substring in Python - GeeksforGeeks
December 20, 2025 - The index() method works similarly to find() but raises a ValueError if the substring does not exist in the string. It is useful when you need the exact position of the substring and want an explicit error if itโ€™s missing. ... If 'Kingdom' does not exist, it raises a ValueError. Python - Test if string contains element from list
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to check if a python string contains an element from a list
5 Best Ways to Check if a Python String Contains an Element from a List - Be on the Right Side of Change
February 26, 2024 - This code defines a function contains_substring() that takes a string and a list of elements as parameters. It loops through the list, and for each element, checks if it is present in the string using the in keyword.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-finding-strings-with-given-substring-in-list
Finding Strings with Given Substring in List - Python - GeeksforGeeks
July 11, 2025 - a = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] b = 'Geek' # Substring res = [i for i in a if b in i] print(res) ... Explanation: list comprehension iterate through a, adding strings that contain the substring b to the list res.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to detect if a string contains a substring of a list (or dictionary)
How to detect if a string contains a substring of a list (or dictionary) : r/learnpython
July 13, 2021 - If you're interested in doing it that way instead, I would do it something like the following: # Import the regular expression package import re def check(chat_string): # Iterate over each word in the banned list for banned_word in my_list: # use re to find if a word # in the string is present in the banned list if re.findall(banned_word, chat_string, re.I): return(True) # make sure this line is outside of for loops return(False)
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-string-contains
Python String Contains โ€“ See if String Contains a Substring โ€“ LearnDataSci
The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ check-if-a-string-contains-an-element-from-a-list-in-python
Check if a String Contains an Element from a List in Python
October 6, 2023 - In our case, we can use list comprehension to create a new list that contains the elements from my_list that are found in my_string. Here's how to do it: ... No spam ever. Unsubscribe anytime. Read our Privacy Policy. my_string = "Hello, World!" my_list = ["Hello", "Python", "World"] found_elements = [element for element in my_list if element in my_string] print(found_elements)
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-5370.html
Ho to check if string contains substring from list
September 30, 2017 - i want to check if a string contains a substring from a list of string i provide, at the moment i am doing this: if 'U19' in echipa1 or 'U20' in echipa1 or 'U17' in echipa1 or 'U21' in echipa1 : print
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ wzo2s8ib โ€บ python-string-contains-example
How to check if a string contains a substring in Python?
December 20, 2022 - Python's built-in "str" module ... easiest and fastest way to check if a string contains a substring is to use the Python "in" and "not in" operators....
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-check-if-string-contains-substring
Python: Check if String Contains Substring
June 20, 2023 - To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!") This operator is shorthand for calling an object's __contains__ ...
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ how-to-check-if-a-string-contains-a-substring-in-python
How to Check if a String Contains a Substring in Python | Codecademy
... Since the substring is present in the string and it starts from index 17, the code produces this output: ... Letโ€™s move on to the next method on the list, which is the Python .contains() method.
๐ŸŒ
Hackr
hackr.io โ€บ home โ€บ articles โ€บ programming
How To Check If A Python String Contains a Substring
August 1, 2024 - How to find if a Python string contains a substring? I cover everything you need to know about finding Python substrings, including code examples and more.
๐ŸŒ
Real Python
realpython.com โ€บ python-string-contains-substring
How to Check if a Python String Contains a Substring โ€“ Real Python
December 1, 2024 - The .str.contains() method in pandas ... entries contain a specific substring. Understanding these methods and tools enables you to effectively check for substrings in Python strings, catering to various needs from simple checks to complex data analysis. Get Your Code: Click here to download the free sample code that youโ€™ll use to check if a string contains ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-check-if-a-substring-is-a-part-of-list-of-strings-using-python
How to Check if a substring is a part of List of Strings using Python?
August 7, 2023 - Finally, we use the any() function to check if any element in match_list is True i.e. we return True otherwise, we return False. def is_substring_present_listcomp(substring, string_list): match_list = [substring in string for string in string_list] return any(match_list) res = is_substring...