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,

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.

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