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,

Advertisements
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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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