Last updated: Apr 9, 2024
Reading timeยท7 min
To check if a string starts with any element from a list:
tuple()
class to convert the list to a tuple.startswith()
method to check if the string starts with any element
in the tuple.startswith
method will return True
if the condition is met.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)))
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.
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.
The any
function will return True
if the string starts with any element from
the list, otherwise False
is returned.
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.
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.
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.
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))
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
.
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
.
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))
Use the any()
function to check if any element in a list starts with a
specific string.
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any(item.startswith('Ki') for item in my_list): # ๐๏ธ this runs print('There is a list item that starts with Ki') else: print('No list items start with Ki') # ๐๏ธ True print(any(item.startswith('Ki') 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.
In the example, we check if each item in the list starts with the string Ki
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 element that starts with the specified string.
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any((match := item).startswith('Ki') for item in my_list): # ๐๏ธ this runs print('There is a list item that starts with Ki') print(match) # ๐๏ธ Kiwi else: print('No list items start with Ki')
Assignment expressions allow us to assign to variables within an expression
using the NAME := expression
syntax.
The
str.startswith
method returns True
if the string starts with the provided prefix, otherwise
the method returns False
.
Note that the str.startswith()
method performs a case-sensitive comparison.
If you need to check if any element in a list starts with a string ignoring its case, convert both strings to lowercase.
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any(item.lower().startswith('KI'.lower()) for item in my_list): # ๐๏ธ this runs print('There is a list item that starts with Ki') else: print('No list items start with Ki') # ๐๏ธ True print(any(item.lower().startswith('KI'.lower()) for item in my_list))
str.lower()
method to convert each list item and the string we are checking for to lowercase.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
.
my_list = ['Apple', 'Banana'] if any(item.startswith('Ki') for item in my_list): print('There is a list item that starts with Ki') else: # ๐๏ธ this runs print('No list items start with Ki') # ๐๏ธ False print(any(item.startswith('Ki') for item in my_list))
None of the items in the list starts with Ki
, so the condition is never met
and any()
returns False
.
If your list is empty, the any
function will always return False
.
my_list = [] if any(item.startswith('Ki') for item in my_list): print('There is a list item that starts with Ki') else: # ๐๏ธ this runs print('No list items start with Ki') # ๐๏ธ False print(any(item.startswith('Ki') for item in my_list))
To find the list items that start with a given string:
str.startswith()
method to check if each item starts with the
string.a_list = ['apple', 'application', 'example'] string = 'app' new_list = [item for item in a_list if item.startswith(string)] print(new_list) # ๐๏ธ ['apple', 'application']
We used a list comprehension to iterate over the list.
On each iteration, we use the str.startswith()
method to check if the current
item starts with the given string.
The
str.startswith
method returns True
if the string starts with the provided prefix, otherwise
the method returns False
.
The new list only contains the items that meet the condition.
If you need to find the list items that start with a given string in a
case-insensitive manner, use the str.lower()
method.
a_list = ['apple', 'application', 'example'] string = 'APP' new_list = [item for item in a_list if item.lower().startswith(string.lower())] print(new_list) # ๐๏ธ ['apple', 'application']
The str.lower method returns a copy of the string with all the cased characters converted to lowercase.
By converting both of the strings to lowercase, we check if the string starts with the substring in a case-insensitive manner.
Alternatively, you can use a for loop.
This is a three-step process:
for
loop to iterate over the list.str.startswith()
method to check if each item starts with the given
string.a_list = ['apple', 'application', 'example'] string = 'app' new_list = [] for item in a_list: if item.startswith(string): new_list.append(item) print(new_list) # ๐๏ธ ['apple', 'application']
We used a for
loop to iterate over the list.
On each iteration, we check if the current item starts with the specified string.
If the condition is met, we append the item to a new list.
The list.append() method adds an item to the end of the list.
break
statement once you encounter a match.a_list = ['apple', 'application', 'example'] string = 'app' match = None for item in a_list: if item.startswith(string): match = item break print(match) # ๐๏ธ 'apple'
If we find an item that starts with the given string, we assign the value to a variable and break out of the loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
You can learn more about the related topics by checking out the following tutorials: