I'd compile the list into a fnmatch pattern:

import fnmatch

pattern = '*'.join(contains)
filetered_filenames = fnmatch.filter(master_list, pattern)

This basically concatenates all strings in contains into a glob pattern with * wildcards in between. This assumes the order of contains is significant. Given that you are looking for prefixes, suffixes and (parts of) dates in between, that's not that much of a stretch.

It is important to note that if you run this on an OS that has a case-insensitive filesystem, that fnmatch matching is also case-insensitive. This is usually exactly what you'd want in that case.

Answer from Martijn Pieters on Stack Overflow
🌐
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)?

Discussions

Does Python have a string 'contains' substring method? - Stack Overflow
I'm looking for a string.contains or string.indexof method in Python. ... Note: This is case-sensitive. ... Sign up to request clarification or add additional context in comments. ... The find() method should be used only if you need to know the position of sub. To check if sub is a substring or ... More on stackoverflow.com
🌐 stackoverflow.com
Checking whether multiple substrings are within a string?
You could use the builtin any function and a generator expression - something like this: if any(s in f for s in ("_F_", "_F.", "_R_","_R.")): print("Success") More on reddit.com
🌐 r/learnpython
9
2
October 24, 2016
What is the most efficient way to find substrings in strings?
Easiest solution is to just use the 'in' operator like this: if "Leonardo DiCaprio" in article_title: return True And according to the top answer here it's also the fastest: whats-a-faster-operation-re-match-search-or-str-find Edit:cant get that return statement to indent for the life of me, you get the idea More on reddit.com
🌐 r/learnpython
22
4
January 11, 2022
How do I check if string contains all substrings in list?

You could try

if all([val in string for val in list]):

Which checks the truth of each check and returns True only if all are True. Otherwise you're looking at a loop (or if your lists get really big a generator).

More on reddit.com
🌐 r/learnpython
11
1
November 16, 2018
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - The in membership operator is the recommended way to check if a Python string contains a substring. Converting input text to lowercase generalizes substring checks by removing case sensitivity. The .count() method counts occurrences of a substring, while .index() finds the first occurrence’s position. Regular expressions in the re module allow for advanced substring searches based on complex conditions.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-all-substrings-of-given-string
Python - Get all substrings of given string - GeeksforGeeks
July 12, 2025 - Print the result: The resulting list substrings contains all substrings of the string s, and it is printed. Let's explore various other methods to Get all substrings of a given strings: ... Nested loops in Python allow you to iterate over multiple levels of data, making them useful for tasks ...
🌐
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 ...
🌐
CodeFatherTech
codefather.tech › home › blog › how to check if a python string contains a substring
How to Check if a Python String Contains a Substring - Codefather
December 8, 2024 - It’s very common having to check if a string contains multiple substrings. Imagine you have a document and you want to confirm, given a list of words, which ones are part of the document. In this example we are using a short string but imagine the string being a document of any length. document = "The Python programming language was created by Guido van Rossum" words = ["Python", "Rossum", "substring"]
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
Search for a String in Python (Check If a Substring Is Included/Get a Substring Position) | note.nkmk.me
May 7, 2023 - For more complex operations, consider using regular expressions, as described in the following sections. Note that the in operator can also be used for lists, tuples, and dictionaries. See the following article for details.
🌐
Udemy
blog.udemy.com › home › it & development › software development › the many ways to check if a python string contains a substring
Ways to Check if a Python String Contains a Substring - Udemy Blog
April 14, 2026 - Angela Yu, Developer and Lead Instructor ... If all you want to do is count the amount of occurrences of a substring in a string, then it’s best to use the count method of the Python str class.
🌐
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
🌐
FavTutor
favtutor.com › blogs › string-contains-substring-python
Check if Python string contains substring: 3 Methods (with code)
January 23, 2023 - You can use the .findall() function instead of .search() function to find every matching substring that ends with punctuation. Python has __contains__() as an instance method to check for substrings in a string.
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › python-string-contains
How does Python string contains substring method?
In python string contains: you can check whether a string contains a substring using the in keyword or the str.find() method.
🌐
Analytics Vidhya
analyticsvidhya.com › home › learn how to check if a string contains a substring in python
Learn How to Check If a String Contains a Substring in Python - Analytics Vidhya
January 10, 2024 - To check if a string contains multiple ... = ["Hello", "Python"] if all(substring in string for substring in substrings): print("All substrings found!") else: print("One or more substrings not found.")...
🌐
Hackr
hackr.io › home › articles › programming
How To Check If A Python String Contains a Substring
August 1, 2024 - Now, I did promise that I’d be thorough and cover some more methods for finding whether a Python string contains a substring. That said, I’d still recommend that you stick to using the membership test with the in keyword. With all that out of the way, let’s take a look at how we can use the .find() and .split() methods for finding a substring.
🌐
Stack Abuse
stackabuse.com › python-check-if-string-contains-substring
Python: Check if String Contains Substring
June 20, 2023 - The easiest way to check if a Python string contains a substring is to use the in operator.
🌐
ReqBin
reqbin.com › code › python › wzo2s8ib › python-string-contains-example
How to check if a string contains a substring in Python?
December 20, 2022 - To count the number of substrings in a string, you can use the string.count() method. ... The most advanced method for working with text in Python is using regular expressions. Regular expressions allow you to quickly check if a string 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
Next, let’s go through an example that uses the .contains() method to check for a substring in a string: ... In all the methods that we’ve discussed so far, we’ve kept matching cases for strings and substrings.
🌐
Initial Commit
initialcommit.com › blog › python-string-contains
Check If Python String Contains Substring - Initial Commit
April 8, 2021 - The quickest way to determine whether or not a Python string contains a substring is to use the in operator. The result will be a boolean value telling you whether or not the substring is "in" the parent string.
🌐
freeCodeCamp
freecodecamp.org › news › python-string-contains-python-3-substring-tutorial-2
Python String Contains – Python 3 Substring Tutorial
November 17, 2022 - Using the in operator is one of the clearest, most readable, and most Pythonic ways to confirm whether a substring is present in a string. Therefore, the in operator is the preferred way of checking that a string contains a substring.