Change language

Python | Check if a string ends with any string in the specified list

|

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

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method