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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

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

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically