Skip to Content

Python – Check If String Starts with any in a List

In this tutorial, we will look at how to check if a string in Python starts with any value in a specified list with the help of some examples.

How to check if a string starts with any item in a list?

check if string starts with a value in a list in python

To check if a string in Python starts with any value in a list, check if the first character in the string is present in the given list using the membership operator in.

The membership operator in in Python, returns True if a value is in a collection (for example, a character is present in a list) and returns False otherwise.

The following is the syntax –

# check if string starts with any in list
s[0] in ls

It returns True if the string s starts with a value that is present in the list ls.

Examples

Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.

# create strings
s1 = "apple"
s2 = "7eleven"
s3 = "$500"
s4 = ""

# display the strings
print(s1)
print(s2)
print(s3)
print(s4)

Output:

apple
7eleven
$500

Here, we created four strings – s1, s2, s3 and s4. The string s1 starts with the letter 'a', the string s2 starts with '7', the string s3 starts with the special character '$' and the string s4 is an empty string.

Let’s now check if the strings s1, s2, and s3 start with a value that is present is a specified list ls or not.

# create a list of accepted starting values
ls = ["a", "z", "7"]

# check if string starts wtih a value in ls
print(s1[0] in ls)
print(s2[0] in ls)
print(s3[0] in ls)

Output:

True
True
False

We get True for s1 and s2 as they start with values that are present in the list ls. We get False s3 as it does not start with a value that is present in the list ls.

Now let’s apply the above method to the empty string, s4.

# check if string starts wtih a value in ls
print(s4[0] in ls)

Output:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [3], in <module>
      1 # check if string starts wtih a value in ls
----> 2 print(s4[0] in ls)

IndexError: string index out of range

We get an IndexError because we’re trying to access an index that does not exist (an empty string does not have a 0 index).

To avoid the above error, you can first check whether the string is non-empty and then proceed to check if the first character is in the given list or not. See the example below –

# function to check if string starts with a value in ls
def check_str_starts_with_ls_val(s, ls):
    if s and s[0] in ls:
        return True
    else:
        return False

# check if string starts with a value in ls
print(check_str_starts_with_ls_val(s4, ls))

Output:

False

We get False as the output for the empty string.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.