Python - Check if string starts with any element in list Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We need to check if a given string starts with any element from a list of substrings. Let's discuss different methods to solve this problem.Using startswith() with tuplestartswith() method in Python can accept a tuple of strings to check if the string starts with any of them. This is one of the most efficient ways to solve the problem. Python s = "geeksforgeeks" prefixes = ["geek", "for", "python"] # Checking if the string starts with any element in the list res = s.startswith(tuple(prefixes)) print(res) OutputTrue Explanation:String 's' is checked against a tuple of substrings from prefixes using startswith().startswith() method returns True if 's' starts with any of the substrings.The result is stored in res and printed.Let's explore some more methods and see how we can check if string starts with any element in list.Table of ContentUsing any() and startswith()Using a loopUsing regular expressionsUsing filter() and lambdaUsing any() and startswith()We can use the any() function to combine the startswith() check for all elements in the list into a single line. Python s = "geeksforgeeks" prefixes = ["geek", "for", "python"] # Using any to check for prefixes res = any(s.startswith(prefix) for prefix in prefixes) print(res) OutputTrue Explanation:any() function evaluates a generator expression that checks if 's' starts with each prefix.If any prefix matches, the any() function returns True.This approach is concise and avoids the need for an explicit loop.Using a loopWe can iterate through the list of substrings and check if the string starts with any of them using the startswith() method. Python s = "geeksforgeeks" prefixes = ["geek", "for", "python"] res = False # Iterating through the list of prefixes for prefix in prefixes: if s.startswith(prefix): # Checking if the string starts with the current prefix res = True break # Exiting the loop as soon as a match is found print(res) OutputTrue Explanation:We initialize res as False to indicate no match initially.The for loop iterates through each substring in prefixes and checks if 's' starts with it.If a match is found, res is set to True, and the loop is terminated.Using regular expressionsFor more advanced cases, regular expressions can be used to match prefixes in the string. Python import re s = "geeksforgeeks" prefixes = ["geek", "for", "python"] # Creating a pattern from the list of prefixes pattern = f"^({'|'.join(re.escape(prefix) for prefix in prefixes)})" res = bool(re.match(pattern, s)) print(res) Explanation:We construct a regular expression pattern to match any of the prefixes at the start of the string.re.match() function checks if the string matches the pattern.The result is converted to a boolean to indicate whether there is a match.Using filter() and lambdaWe can also use the filter() function along with a lambda to find if any prefix matches. Python s = "geeksforgeeks" prefixes = ["geek", "for", "python"] # Using filter and lambda to find matches res = bool(list(filter(lambda prefix: s.startswith(prefix), prefixes))) print(res) # OutputTrue Explanation:lambda function checks if 's' starts with each prefix.filter() function applies this check to all elements in prefixes and returns matches.We convert the result to a boolean to determine if any match exists. Comment More infoAdvertise with us Next Article Python - Check if string starts with any element in list manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati 6 min read Python | Check if suffix matches with any string in given list Given a list of strings, the task is to check whether the suffix matches any string in the given list. Examples: Input: lst = ["Paras", "Geeksforgeeks", "Game"], str = 'Geeks' Output: TrueInput: lst = ["Geeks", "for", "forgeeks"], str = 'John' Output: False Let's discuss a few methods to do the task 6 min read Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio 2 min read Python | Checking if starting digits are similar in list Sometimes we may face a problem in which we need to find a list if it contains numbers with the same digits. This particular utility has an application in day-day programming. Let's discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach t 8 min read Python - Check if any list element is present in Tuple Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements. 6 min read Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an 3 min read Finding Strings with Given Substring in List - Python The task of finding strings with a given substring in a list in Python involves checking whether a specific substring exists within any of the strings in a list. The goal is to efficiently determine if the desired substring is present in any of the elements of the list. For example, given a list a = 3 min read Extract words starting with K in String List - Python In this article, we will explore various methods to extract words starting with K in String List. The simplest way to do is by using a loop.Using a LoopWe use a loop (for loop) to iterate through each word in the list and check if it starts with the exact character (case-sensitive) provided in the v 2 min read Python - Check if List contains elements in Range Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function - 3 min read Python - Start and End Indices of words from list in String Given a String, our task is to write a Python program to extract the start and end index of all the elements of words of another list from a string. Input : test_str = "gfg is best for all CS geeks and engineering job seekers", check_list = ["geeks", "engineering", "best", "gfg"]Output : {'geeks': [ 7 min read Like