What is Python endsWith() Function?

What is Python endsWith() Function?

4 mins read103 Views Comment
Updated on Mar 30, 2023 02:36 IST

In Python, the endswith() function for a String object determines whether the string ends with a specified suffix, and returns True if it does, or False if it does not.

2023_03_Understanding-Break-Statement-in-C-25.jpg

In this article, we will discuss the Python endswith() function and understand its use with the help of examples. We will be covering the following sections:

What is Python endswith() Function?

The endswith() function is a built-in method in Python‘s String class, which is used to determine whether a given string ends with a specific suffix. It is a very useful function for string processing and can be used in a variety of situations. The endswith() function takes one or more arguments, including the suffix or suffixes to be checked, and returns a Boolean value of True or False, depending on whether the string ends with the specified suffix. This function is often used in combination with other string processing functions to manipulate and analyze strings in Python.

Explore free Python courses

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Syntax of endswith()

The syntax of endswith() function in Python is given as:


 
string.endswith(suffix[, start[, end]])
Copy code

Parameters of endswith()

The function endswith() in Python takes the following parameters:

  • string: which is the string to be checked for a given suffix.
  • suffix: which is a mandatory parameter that is searched for in the string.

There are two optional parameters:

  • start: is the starting position for the search.
  • End: is the ending position.

If these optional parameters are not specified, the search will begin at position 0 and end at the last index of the string.

Return Value of endswith()

The endswith() function returns a Boolean value of True if the string ends with the specified suffix, and False otherwise.

Examples of Using the endswith() Function in Python

Example 1:


 
string = "Hello, John!"
result = string.endswith("!")
print(result)
Copy code

Output:


 
True
Copy code

In this example, we have assigned a string to a variable “string”. We then use the endswith() function to check whether the string ends with an exclamation mark (“!”). Since the string ends with “!”, the endswith() function returns True, which is stored in the “result” variable. The output of the program is “True”.

Example 2:


 
string = "Hello, learners"
result = string.endswith("learners")
print(result)
Copy code

Output:


 
True
Copy code

In this example, we are checking whether the string ends with the word “learners” using the endswith() function. Since the string does end with “learners”, the function returns True, and the output of the program is “True”.

Example 3:


 
string = "Welcome home"
result = string.endswith("home", 7, 12)
print(result)
Copy code

Output:


 
True
Copy code

In this example, we are searching for the word “home” within the string starting at position 7 and ending at position 12 (exclusive). The endswith() function checks whether the substring within this range ends with “ho”. Since it does, the function returns True, and the output of the program is “True”.

Example 4:

In Python, the endswith() method can also be used with a tuple of suffixes. When used with a tuple, the method checks if the string ends with any one of the suffixes in the tuple. Here’s an example:


 
string = "Hello, world!"
suffixes = (".com", "rld!", "world")
result = string.endswith(suffixes)
print(result)
Copy code

Output:


 
True
Copy code

In this example, we have a string “Hello, world!”. We have defined a tuple “suffixes” containing three suffixes: “.com”, “rld!”, and “world”. We then use the endswith() method to check whether the string ends with any of these suffixes. Since the string ends with “world”, which is one of the suffixes in the tuple, the endswith() method returns True. Therefore, the output of the program is “True”.

The endswith() method works by iterating through the tuple of suffixes, and calling the string.endswith() method for each suffix. It returns True if any one of the suffixes matches the end of the string, and False otherwise. The method stops iterating through the tuple as soon as it finds a matching suffix. If none of the suffixes match the end of the string, the method returns False.

Endnotes

Hope this article was helpful for you to understand how and why the endswith() method is used in Python. If you wish to learn more about Python and practice Python programming, you can explore related articles here.

FAQs

What is the difference between the endswith() and startswith() methods in Python?

The endswith() method checks if a string ends with a given suffix, whereas the startswith() method checks if a string starts with a given prefix.

Can the endswith() method be used with case-insensitive suffixes?

Yes, the endswith() method can be used with case-insensitive suffixes. To do this, you can convert the string and the suffix to lowercase (or uppercase) before passing them to the method, like this: string.lower().endswith(suffix.lower()).

Can the endswith() method be used with regular expressions?

No, the endswith() method cannot be used with regular expressions. However, you can use the re module in Python to match regular expressions against strings.

Does the endswith() method modify the original string?

No, the endswith() method does not modify the original string. It simply returns a Boolean value indicating whether the string ends with the given suffix.

Can the endswith() method be used with non-string types?

No, the endswith() method can only be used with string types in Python. If you need to check if an object ends with a certain sequence, you may need to convert it to a string first.

Can the endswith() method be used with multiple suffixes?

Yes, the endswith() method can be used with multiple suffixes by passing them as a tuple. The method will return True if the string ends with any one of the suffixes in the tuple.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio