Python String endswith() Method – Tutorial with Examples

Python String endswith() Method

The endswith() method is a built-in method in Python that returns a boolean value indicating whether the string ends with a specified suffix. It can be used to check if a given string ends with a specific character or group of characters.

Syntax

The syntax of the endswith() method is as follows:

string.endswith(suffix, start, end)

Here, string is the string to be checked, suffix is the string or character(s) to check for at the end of the string, start is the starting index to consider in the string, and end is the ending index to consider in the string.

The suffix parameter is mandatory, while the start and end parameters are optional.

Return Value

The endswith() method returns a boolean value True if the given string ends with the specified suffix, and False otherwise.

Examples

Here are three different examples of how to use the endswith() method in Python:

Example 1: Checking if a String Ends with a Specified Suffix

The following example demonstrates how to use the endswith() method to check if a string ends with a specific suffix:

string = "Hello, world!"
result = string.endswith("world!")
print(result)

Output:

True

In this example, we check if the string “Hello, world!” ends with the suffix “world!” using the endswith() method. The method returns True, as the string does indeed end with the specified suffix.

Example 2: Checking if a String Ends with Multiple Possible Suffixes

The following example demonstrates how to use the endswith() method to check if a string ends with multiple possible suffixes:

string = "Hello, world!"
result1 = string.endswith("world!")
result2 = string.endswith(("planet!", "world!"))
print(result1)
print(result2)

Output:

True
True

In this example, we first check if the string “Hello, world!” ends with the suffix “world!” using the endswith() method. The method returns True. We then check if the string ends with either “planet!” or “world!” using the same method with a tuple of suffixes. The method again returns True, as the string ends with one of the specified suffixes.

Example 3: Checking if a String Ends with a Suffix within a Specified Range

The following example demonstrates how to use the endswith() method to check if a string ends with a specified suffix within a certain range:

string = "Hello, world!"
result = string.endswith("world", 7, 13)
print(result)

Output:

True

In this example, we check if the substring “world” at indices 7 to 13 of the string “Hello, world!” using the endswith() method. The method returns True, as the substring ends with the specified suffix “world”.

Use Cases

The endswith() method can be useful in a variety of use cases, such as:

  • Checking if a file has a certain file extension
  • Validating input from a user to ensure it ends with a certain character or string
  • Filtering a list of strings to only include those that end with a specific suffix

Overall, the endswith() method is a handy tool for checking the end of a string in Python and can be used in many different situations.

Leave a Reply

Your email address will not be published. Required fields are marked *