Python String endswith() Method

Python String Endswith() Method

This is a tutorial of the Python String endswith() method. Learn to check if a given string ends with a given suffix or substring or not with examples.

Python String endswith()

The string.endswith() method returns True if a given string ends with a particular suffix or substring and False otherwise.

Note. Similar to this method, there’s also another method string.startswith() using which you can check if a given string starts with a particular prefix or substring or not.


The following is the simple usage representation of the above syntax.

endswith() Parameters

The endswith() method of Python String can take three different arguments. Only the first argument is mandatory while the rest two are optional. These are defined below.

  • suffixString. The suffix which you want to check in the string is needed to be provided as the first argument to this method. It is the only mandatory parameter. It must be passed as a String or a Tuple containing various strings (suffixes) as items.
  • start and end. These are the optional parameters and it defines the starting and ending indexes of the substring to be searched for checking the suffix. These are basically the numerical indexes of the different characters in the main string. For Example, let’s say the ending index is 5. Then this method will check if the last character of the provided suffix is present at the 5th index. If it will be, it will return True, otherwise False. In other words, by providing these start and end arguments, you basically shorten the string to be checked for the suffix.

Have a look at the examples to better understand the different use cases for these parameters.

endswith() Return Value

As we make use of this function just to check if the string ends with the provided suffix or not, so it returns a boolean value, i.e. either True or False.

  • It will return True if the provided suffix is the substring with which the string ends.
  • It will return False if the string does not end with the provided suffix or substring.

Checking multiple Suffixes for a Given String using Python Tuples

Instead of passing a single suffix to check if a given string ends with it or not. You can also pass a tuple as an argument to this method that contains multiple suffixes to check if the given string ends with any of the tuple-contained suffixes.

The syntax remains almost the same, it’s just that instead of passing the first parameter as a simple string containing a single suffix, here you’ve to provide a tuple containing different string type suffixes as its items.

In case, you’ve provided a tuple as the first argument to the Python String endswith() method, it will return True if the given string ends with any of the string contained as the Tuple element and if the given string does not end with any of the string elements of the Tuple, it will return False.


Given below are the two examples demonstrating the usage of the Python String endswith() method.

Example 1. endswith() Basic Usage without start and end arguments

#Basic usage
string = "Python is quite popular these days in the programming world."

#String ends with the specified Suffix
#returns True


#String does not ends with the specified Suffix
print(string.endswith("Hello World!"))

#Using If-Else Statement
if string.endswith("Suffix"):
    print("String ends with Suffix.")
    print("String does not end with Suffix.")


String does not end with Suffix.

Example 2. endswith() Usage with start and end arguments

#Usage of endswith() along with start and end arguments
string = "Python is quite popular these days in the programming world."

#Lookedup String: popular these days in the programming world.
print(string.endsswith("popular", 16))
#returns True

#Lookedup String: quite popular
print(string.endswith("Python", 10, 22))
#returns False

#Negative Indexing
#Lookedup String: world.
print(string.endswith("world.", -6))

#Lookedup String: programming world
print(string.endsswith("program", -18, -1))



Example 3. Passing Tuple of Suffixes in endswith()

#Checking multiple suffixes for a single string
string = "Python is a cool programming Language"

#Passing Tuple of Suffixes as an argument
#return True

#return False

print(string.endswith(("is"," ","a","cool","Language","programming"), 12, 30))
#return False

print(string.endswith(("is"," ","a","cool","Language","programming"), -10))
#return True



I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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