How to check whether a string ends with one from a list of suffixes in Python?


A suffix is defined as a letter or group of letters added at the end of a word to make a new word.

Let's say you have a list of suffixes, which are groups of letters that can be added to the end of a word to change its meaning. You want to check if a given string ends with one of these suffixes in Python.

To check if a string ends with a suffix in a list

Example

In this example, we first define a list of suffixes that we want to check if the string ends with. We then ask the user to enter a string using the input() function.

We then use a for loop to iterate over each suffix in the list of suffixes. We use the endswith() method to check if the input string ends with the current suffix in the loop.

If the input string ends with the current suffix, we print a message indicating which suffix it ends with and exit the loop using the break statement.

If none of the suffixes in the list match the end of the input string, we print a message indicating that the string does not end with any of the suffixes.

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

for suffix in suffixes:
    if input_str.endswith(suffix):
        print(f"The string ends with {suffix}")
        break
else:
    print("The string does not end with any of the suffixes")

Output

Enter a string: Wanted
The string ends with ed

Using list comprehension

Example

In this example, we use a list comprehension to create a list of suffixes that the input string ends with. We iterate over each suffix in the list of suffixes and use the endswith() method to check if the input string ends with the current suffix.

We then check if the resulting list is not empty using an if statement. If the list is not empty, we print a message indicating which suffix the string ends with. If the list is empty, we print a message indicating that the string does not end with any of the suffixes.

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

result = [suffix for suffix in suffixes if input_str.endswith(suffix)]

if result:
    print(f"The string ends with {result[0]}")
else:
    print("The string does not end with any of the suffixes")

Output

Enter a string: Slowly
The string ends with ly

Using any() function

Example

In this example, we use the any() function to check if the input string ends with any of the suffixes in the list of suffixes. We pass a generator expression to the any() function that iterates over each suffix in the list and checks if the input string ends with that suffix.

If any of the suffixes match the end of the input string, we print a message indicating that the string ends with one of the suffixes in the list. If none of the suffixes match the end of the input string, we print a message indicating that the string does not end with any of the suffixes.

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

if any(input_str.endswith(suffix) for suffix in suffixes):
    print(f"The string ends with one of the suffixes in {suffixes}")
else:
    print("The string does not end with any of the suffixes")

Output

Enter a string: Monalisa
The string does not end with any of the suffixes

Using filter() function

Example

In this example, we use the filter() function to create a new list that only contains suffixes that the input string ends with. We pass the endswith() method as the filter function and the list of suffixes as the iterable to be filtered.

We then convert the filtered suffixes into a list using the list() function and assign it to the result variable.

If the result list is not empty, we print a message indicating which suffix the string ends with. If the result list is empty, we print a message indicating that the string does not end with any of the suffixes.

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

filtered_suffixes = filter(input_str.endswith, suffixes)
result = list(filtered_suffixes)

if result:
    print(f"The string ends with {result[0]}")
else:
    print("The string does not end with any of the suffixes")

Output

Enter a string: Surfing
The string ends with ing

Using a regular expression

Example

In this example, we use a regular expression to check if the input string ends with any of the suffixes in the list of suffixes. We first import the re module, which provides support for regular expressions in Python.

We then define a regular expression pattern that matches any characters before the suffixes in the list and ends with one of the suffixes. We use a formatted string literal (f-string) to create the regular expression pattern dynamically based on the suffixes in the list.

We then use the re.match() function to check if the input string matches the regular expression pattern. If the input string matches the pattern, we print a message indicating that the string

import re
suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

regex_pattern = fr".*({'|'.join(suffixes)})$"

if re.match(regex_pattern, input_str):
    print(f"The string ends with one of the suffixes in {suffixes}")
else:
    print("The string does not end with any of the suffixes")

Output

Enter a string: Lily
The string ends with one of the suffixes in ['ing', 'ed', 'ly']

Updated on: 10-Aug-2023

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements