Check if string starts with any string from List in Python

In this Python tutorial, we will discuss how to check if a string starts with one of the strings from a list.

Table Of Contents

Check if string starts with any string from List using startswith() method

In Python, the string class provides a function startswith(), to check if the string starts with a given substring or not. The startswith() function accepts either a string or a tuple as an argument,

  • It a string is provided, then it checks if the calling string object starts with the given string or not.
  • If a tuple of strings is provided, then it checks if the calling string object starts with one of the string in given tuple or not.

So, we can convert our list of substrings to a tuple and pass them to the startswith() function. If it returns True, then it means the string starts with one of the string from the list.

Let’s see an example,

listOfSubstrings = ["Why", "What", "Hello"]

strValue = "Hello thisPointer"

# Check if string starts with any string from the list
if strValue.startswith(tuple(listOfSubstrings)):
    print('String starts with a string from the list')
else:
    print('String does not starts with a string from the list')

Output:

String starts with a string from the list

Check if string starts with any string from List using filter() and startswith()

Select only those strings from the list which exists at the start of the main string. If number of such strings is 0, then it means the given string does not starts with any string from the list.

Use filter() and startswith() functions to filter only those strings from list which starts with a string from a list. Basically using the filter() function, we will iterate over all the strings in list and for each string we will check if it exists at the start of the main string or not.

The complete example is as follows,

listOfSubstrings = ["Why", "What", "Hello"]

strValue = "Hello thisPointer"

# Check if string starts with any string from the list
if list(filter(strValue.startswith, listOfSubstrings)) != []:
    print('String starts with a string from the list')
else:
    print('String does not starts with a string from the list')

Output:

String starts with a string from the list

Check if the string starts with one of the strings from a List using the in operator

Split the main string into a list of substring. Then select the first element of that list and check if it exists in the another list or not. If yes, then it means that the main string starts with any string from the list.

Let’s see an example,

listOfSubstrings = ["Why", "What", "Hello"]

strValue = "Hello thisPointer"
splittedStrs = strValue.split()

# Check if string starts with any string from the list
if len(splittedStrs) > 0 and splittedStrs[0] in listOfSubstrings:
    print('String starts with a string from the list')
else:
    print('String does not starts with a string from the list')

Output:

String starts with a string from the list

We can see that “Hello” exist in the list of 3 strings.

Summary

We learned three methods to check if a string starts with one of the strings from a list. you should first try to use the startswith() method, because it is very easy to implement. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top