Python endswith() Method

python endswith

Python is a popular programming language that is widely used in data science, web development, and automation. It has a rich set of built-in methods that make it easy to manipulate strings, lists, and other data types. One of these methods is endswith(), which is used to check if a string ends with a particular suffix. In this article, we’ll discuss in detail what endswith() is and how to use it effectively.

What is the Python endswith() Method?

The endswith() method is a built-in method in Python that returns a boolean value indicating whether a string ends with a particular suffix or not. It takes one or two arguments, where the first argument is the suffix to be checked, and the second argument is the starting and ending index of the string to be checked. If the starting and ending index is not specified, the method will check the entire string.

The syntax for using the endswith() method is as follows:

string.endswith(suffix, start, end)

Here, string is the string to be checked, suffix is the string to be searched at the end of the string, start is the starting index of the search, and end is the ending index of the search.

How to Use the Python endswith() Method?

Now that we have a basic understanding of what endswith() is, let’s see how to use it with some examples.

Example 1: Using endswith() Method to Check if a String Ends with a Particular Suffix

string = "Hello World!"
print(string.endswith("!"))

Output:

True

In this example, we have a string “Hello World!”. We are using the endswith() method to check if the string ends with the suffix “!”. Since the string does end with “!”, the method returns True.

Example 2: Using endswith() Method with Multiple Suffixes

string = "Hello World!"
print(string.endswith(("o", "d", "!")))

Output:

True

In this example, we have a string “Hello World!”. We are using the endswith() method to check if the string ends with any of the suffixes “o”, “d”, or “!”. Since the string does end with “!”, the method returns True.

Example 3: Using endswith() Method with a Specific Index Range

string = "Hello World!"
print(string.endswith("o", 0, 5))

Output:

True

In this example, we have a string “Hello World!”. We are using the endswith() method to check if the substring starting from index 0 and ending at index 5 (exclusive) ends with the suffix “o”. Since the substring “Hello” ends with “o”, the method returns True.

Example 4: Using endswith() Method with a Negative Index Range

string = "Hello World!"
print(string.endswith("o", -6, -1))

Output:

False

In this example, we have a string “Hello World!”. We are using the endswith() method to check if the substring starting from index -6 and ending at index -1 (exclusive) ends with the suffix “o”. Since the substring “World” does not end with “o”, the method returns False.

Example 5: Using endswith() Method with No Suffix

string = "Hello World!"
print(string.endswith(""))

Output:

True

In this example, we have a string “Hello World!”. We are using the endswith() method to check if the string ends with an empty suffix “”. Since an empty suffix is always present at the end of the string, the method returns True.

Conclusion

The endswith() method is a simple yet powerful method to check if a string ends with a particular suffix or not. It is easy to use and can be helpful in various scenarios. By using the endswith() method, you can quickly check if a file has a particular extension, or if a URL ends with a specific domain name, or if a sentence ends with a particular punctuation mark, and so on.

In addition to endswith(), there are several other string methods in Python that can be used to manipulate strings in different ways. These include startswith(), find(), replace(), split(), and many others. By mastering these methods, you can become proficient in string manipulation and make your Python code more efficient and effective.