Python | Check if string ends with any string in given list
While working with strings, their prefixes and suffix play an important role in making any decision. For data manipulation tasks, we may need to sometimes, check if a string ends with any of the matching strings. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using filter() + endswith()
The combination of the above function can help to perform this particular task. The filter method is used to check for each word and endswith method tests for the suffix logic at target list.
# Python3 code to demonstrate # Checking for string match suffix # using filter() + endswith() # initializing string test_string = "GfG is best" # initializing suffix list suff_list = ['best', 'iss', 'good'] # printing original string print("The original string : " + str(test_string)) # using filter() + endswith() # Checking for string match suffix res = list(filter(test_string.endswith, suff_list)) != [] # print result print("Does string end with any suffix list sublist ? : " + str(res)) |
The original string : GfG is best Does string end with any suffix list sublist ? : True
Method #2 : Using endswith()
As an improvement to the above method, it is not always necessary to include filter method for comparison. This task can be handled solely by supplying a suffix check list as an argument to endswith method as well.
# Python3 code to demonstrate # Checking for string match suffix # using endswith() # initializing string test_string = "GfG is best" # initializing suffix list suff_list = ['best', 'iss', 'good'] # printing original string print("The original string : " + str(test_string)) # using endswith() # Checking for string match suffix res = test_string.endswith(tuple(suff_list)) # print result print("Does string end with any suffix list sublist ? : " + str(res)) |
The original string : GfG is best Does string end with any suffix list sublist ? : True
Recommended Posts:
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if suffix matches with any string in given list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if a given string is binary string or not
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Convert list of string to list of list
- Sum of list (with string types) in Python
- Python | Min/Max value in float string list
- Python | First Non-Empty String in list
- Python | Check for URL in a String
- Python | Get the substring from given string using list slicing
- Python | Splitting list on empty string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



