Check if string starts with any element from List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if string starts with any element from List in Python #

To check if a string starts with any element from a list:

  1. Use the tuple() class to convert the list to a tuple.
  2. Use the startswith() method to check if the string starts with any element in the tuple.
  3. The startswith method will return True if the condition is met.
main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] # ✅ Using str.startswith() with a tuple if my_str.startswith(tuple(my_list)): # 👇️ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # 👇️ True print(my_str.startswith(tuple(my_list))) # -------------------------------------------- # ✅ Using any() with a generator expression if any(my_str.startswith(item) for item in my_list): # 👇️ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # 👇️ True print(any(my_str.startswith(item) for item in my_list))
If you need to perform a case-insensitive comparison, scroll down to the examples that use the any() function.

The first example uses the str.startswith method.

The str.startswith() method returns True if the string starts with the provided prefix, otherwise the method returns False.

The startswith method can be passed a string or a tuple of strings.

If you have a list, make sure to convert it to a tuple by passing it to the tuple() class.

The startswith method will return True if the string starts with any of the strings in the tuple, otherwise False is returned.

Alternatively, you can use the any() function.

Check if string starts with any element from List using any() #

Use the any() function to check if a string starts with any element from a list, e.g. if any(my_str.startswith(item) for item in my_list). The any function will return True if the string starts with any element from the list, otherwise False is returned.

main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] if any(my_str.startswith(item) for item in my_list): # 👇️ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # 👇️ True print(any(my_str.startswith(item) for item in my_list))

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

We passed a generator expression to the any() function.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

In the example, we check if the string starts with each item from the list and return the result.

If the condition is met at least once, the any() function returns True.

You can use the assignment expression syntax if you need to get the list item the string starts with.

main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] if any(my_str.startswith(match := item) for item in my_list): # 👇️ this runs print('string starts with at least one of the elements from the list') print(match) # 👉️ 'he' else: print('string does NOT start with any of the elements from the list')

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

If you need to check if a string starts with any element from a list, in a case-insensitive manner, convert both strings to lowercase.
main.py
my_str = 'HELLO' my_list = ['apple', 'one', 'he'] if any(my_str.lower().startswith(item.lower()) for item in my_list): # 👇️ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # 👇️ True print(any(my_str.lower().startswith(item.lower()) for item in my_list))
We used the str.lower() method to convert the string and each list item to lowercase before calling the startswith method.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

If the iterable we pass to the any() function is empty or none of the elements in the iterable are truthy, the any function returns False.

main.py
my_str = 'hello' my_list = ['apple', 'one'] if any(my_str.startswith(item) for item in my_list): print('string starts with at least one of the elements from the list') else: # 👇️ this runs print('string does NOT start with any of the elements from the list') # 👇️ False print(any(my_str.startswith(item) for item in my_list))

The string doesn't start with any of the items from the list, so the condition is never met and any() returns False.

If your list is empty, the any function will always return False.

main.py
my_str = 'hello' my_list = [] if any(my_str.startswith(item) for item in my_list): print('string starts with at least one of the elements from the list') else: # 👇️ this runs print('string does NOT start with any of the elements from the list') # 👇️ False print(any(my_str.startswith(item) for item in my_list))
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.