Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Check if string ends with any string in given list

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




# 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))


Output

The original string :GfG is best
Does string end with any suffix list sublist ? :True

Time Complexity : O(n)

Space Complexity : O(n)

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




# 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))


Output

The original string :GfG is best
Does string end with any suffix list sublist ? :True

Time Complexity : O(n)

Space Complexity : O(1)

Method #3 : Using split().Splitting the given string and comparing every string of list for matching suffix

Python3




# Python3 code to demonstrate
# Checking for string match suffix
 
# initializing res
res = False
# 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))
 
x = test_string.split()
for i in suff_list:
    if(x[-1] == i):
        res = True
 
# 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

Time Complexity : O(n)

Space Complexity : O(1)

Method #4 : Using split() and in operator

Python3




# Python3 code to demonstrate
# Checking for string match suffix
 
# initializing res
res = False
# 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))
 
x = test_string.split()
if x[-1] in suff_list:
    res=True
# 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

Time Complexity : O(n)

Space Complexity : O(1)

Method #5 : Using find() and len() methods

Python3




# Python3 code to demonstrate
# Checking for string match suffix
 
# Initializing res
res = False
 
# 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 Loop
for i in suff_list:
    a = test_string.find(i)
    b = len(test_string)-len(i)
    if(a == b):
        res = True
 
# 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

Time complexity: O(n * m)

Space complexity: O(1)

Method #6 :  Using any()

The any function in Python returns True if any element in an iterable is True, and False otherwise. In this approach, we use a list comprehension to generate a list of boolean values, where each value is True if the test string ends with the current suffix in the list, and False otherwise. The any function is then used to check if any element in the list is True.

Python3




# Python3 code to demonstrate
# Checking for string match suffix
# initializing string
test_string = "GfG is best"
# initializing suffix list
suffix_list = ['best', 'iss', 'good']
# printing original string
print("The original string :"+ str(test_string))
# Use a list comprehension to check if any suffix in the list appears at the end of the test string
result = any(test_string.endswith(suffix) for suffix in suffix_list)
# print result
print("Does string end with any suffix list sublist ? :"+ str(result))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string :GfG is best
Does string end with any suffix list sublist ? :True

Time complexity: O(n), where n is the length of the suffix list
Auxiliary space: O(1)

Method 7:  using operator.countOf() method

Python3




# Python3 code to demonstrate
# Checking for string match suffix
import operator as op
# initializing res
res = False
# 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))
 
x = test_string.split()
if op.countOf(suff_list, x[-1]) > 0:
    res = True
# 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

Time Complexity: O(N)

Auxiliary Space : O(1)

Method 8: Using regular expressions

Python3




import re
 
# Initializing string
test_string = "GfG is best"
 
# Initializing suffix list
suffix_list = ['best', 'iss', 'good']
# printing original string
print("The original string : " + str(test_string))
# Initializing boolean flag
flag = False
 
# Iterating through suffix list
for suffix in suffix_list:
    # Using regular expression to check suffix match
    match = re.search(r'{}$'.format(suffix), test_string)
    if match:
        flag = True
        break
 
# Print result
print("Does string end with any suffix list sublist ? : " + str(flag))
#This code is contributed by Vinay Pinjala.


Output

The original string : GfG is best
Does string end with any suffix list sublist ? : True

Time Complexity: O(N)

Auxiliary Space : O(1)

Method#9: Using Recursive method.

Python3




def check_suffix(string, suffix_list):
    if not string:
        return False
    words = string.split()
    if not words:
        return False
    if words[-1] in suffix_list:
        return True
    return check_suffix(" ".join(words[:-1]), suffix_list)
 
# initializing res
res = False
# 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))
 
res = check_suffix(test_string, suff_list)
 
# print result
print("Does string end with any suffix list sublist ? : " + str(res))
 
#this code contributed by tvsk


Output

The original string : GfG is best
Does string end with any suffix list sublist ? : True

Time Complexity: O(n)

Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments