Method # 1: Using filter () + endswith ()
A combination of the above function can help accomplish this particular task. The filter method is used to check each word and ends with checking the method for suffix logic in the target list.
# Python3 demo code # Checking the suffix for line matching # using the filter () + ends with () # initialization string test_string = "GfG is best" # initialize suffix list suff_list = [ ’best’ , ’iss’ , ’good’ ] # print original line print ( "The original string:" + str (test_string)) # using the filter () + ends with () # Checking the string suffix res = list ( filter (test_string.endswith, suff_list))! = [] # re print result print ( "Does string end with any suffix list sublist? : " + str (res)) |
Output:
The original string: GfG is best Does string end with any suffix list sublist?: True
Method # 2: Using endswith()
B As an improvement on the above method, it is not always necessary to include a filter
method for comparison. This task can only be accomplished by providing a suffix checklist as an argument to the end method with the method.
# Python3 demo code # Checking string suffixes # usage ends with () # initialization string test_string = "GfG is best " # initialize suffix list suff_list = [ ’best’ , ’ iss’ , ’good’ ] # print original line print ( "The original string:" + str (test_string)) # use ending matched with () # Checking the suffix of line matching res = test_string.endswith ( tuple ( suff_list)) # print result print ( "Does string end with any suffix list sublist? : " + str (res)) |
Output:
The original string: GfG is best Does string end with any suffix list sublist?: True
Python | Check if a string ends with any string in the specified list Python functions: Questions
Python | Check if a string ends with any string in the specified list String Variables: Questions