
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a string ends with one from a list of suffixes in Python?
A suffix is a group of letters added at the end of a word. In Python, we can check if a string ends with any one of multiple suffixes using the endswith() method. It takes a tuple of suffixes as an argument and returns True if the string ends with any of them. This is useful for checking file extensions, URL endings, or word patterns.
Using endswith() with Multiple Suffixes
The endswith() method allows you to check if a string ends with any one of several suffixes by passing them as a tuple. This helps to check for multiple possible endings in a single call.
Example: Check if a String Ends with Any Suffix from a Tuple
In this example, we check if a filename ends with any common document file extensions -
filename = "example_report.docx" suffixes = (".pdf", ".docx", ".txt") result = filename.endswith(suffixes) print(result)
The filename ends with ".docx", so the result is True -
True
Example: Convert List to Tuple for endswith()
In the following example, a list of suffixes is converted to a tuple before passing it to endswith() method -
url = "https://example.com/index.html" suffix_list = [".html", ".php", ".asp"] result = url.endswith(tuple(suffix_list)) print(result)
Here, tuple(suffix_list) converts the list to a tuple, which endswith() can accept -
True
Example: Return False if None Matches
If none of the suffixes match, the method returns False -
filename = "image.jpeg" suffixes = (".png", ".gif", ".bmp") result = filename.endswith(suffixes) print(result)
Following is the output obtained -
False
Using a For Loop
You can use a for loop to go through each suffix and check if the string ends with it by calling the endswith() method for each one. This is useful when suffixes are stored in a list or generated dynamically.
Example: Loop Through List of Suffixes
In this example, we use a loop and check each suffix individually using the endswith() method -
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")
Following is the output obtained -
Enter a string: Wanted The string ends with ed
Using List Comprehension
List comprehension allows you to check multiple suffixes by collecting those that match the end of the string into a new list, all in a single line of code.
Example: Find Matching Suffix with List Comprehension
In this example, we create a list of matching suffixes using list comprehension -
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")
We get the output as shown below -
Enter a string: Slowly The string ends with ly
Using any() Function
The any() function is used to check if the string ends with at least one suffix from a list or tuple. It returns True as soon as a match is found.
Example
In this example, we use any() function with a generator expression to check all 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")
The result obtained is as shown below -
Enter a string: Monalisa The string does not end with any of the suffixes
Using filter() Function
The filter() function is used to find all suffixes that match the end of a string by applying a condition to each one. The matching suffixes are then collected into a list for further use.
Example
In the following example, we use the filter() function to keep only the suffixes that match -
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")
The result produced is as follows -
Enter a string: Surfing The string ends with ing
Using a Regular Expression
We create a regular expression pattern from the list of suffixes and use it to check if the string ends with any of those suffixes. This method allows for pattern matching at the end of the string.
Example
This example uses the re module to dynamically build a regular expression pattern from the list of suffixes -
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")
Following is the output obtained -
Enter a string: Lily The string ends with one of the suffixes in ['ing', 'ed', 'ly']