Check if string ends with any string from List in Python

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

Table Of Contents

Check if string ends with any string from List using endswith() method

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

  • It a string is provided, then it checks if the calling string object ends with the given string or not.
  • If a tuple of strings is provided, then it checks if the calling string object ends 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 endswith() function. If it returns True, then it means the string ends with one of the string from the list.

Let’s see an example,

Advertisements
listOfSubstrings = ["Welcome", "to", "thisPointer"]

strValue = "Hello thisPointer"

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

Output:

String ends with a string from the list

Check if string ends with any string from List using filter() and endswith()

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

Use filter() and endswith() functions to filter only those strings from list which ends 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 end of the main string or not.

The complete example is as follows,

listOfSubstrings = ["Welcome", "to", "thisPointer"]

strValue = "Hello thisPointer"

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

Output:

String ends with a string from the list

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

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

Let’s see an example,

listOfSubstrings = ["Welcome", "to", "thisPointer"]

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

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

Output:

String ends with a string from the list

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

Summary

We learned three methods to check if a string ends with one of the strings from a list. you should first try to use the endswith() 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