Python String Methods Endswith by Manish Sharma

Now that I have shown you how to work with startswith string method in the last tutorial. It is time to move on and learn how to use endswith string method of python. Here I present to you an informative guide to endswith string method of Python Programming.

What is an ‘endswith’ string method?

As the name suggests, the method helps in determining if a string ends with a specified sub-string or not?

[bctt tweet=”Learn how to determine if a string ends with a specified substring or not in Python Programming ” username=”RebellionRider”]

The syntax of ‘endswith’ string method —

Before I show you the examples of endswith string method, it’s good to know about it a little more. So let’s take a quick look at endswith string method of Python Programming.

endswith (‘sub-string’, startIDX, endIDX)

Similar to string method ‘startswith’ the endswith also accepts three parameters. These three parameters are –

Sub-string: The first parameter is the sub-string. This is the string which will get searched into the main string. Substring is a mandatory parameter. Which means, you must specify a sub-string in order to call the method.

startIDX: The second parameter is the start index. It is an integer parameter. The start-index indicates the index number from where the interpreter will start the search. For example, if you specify 12 as startIDX value in the function call then the interpreter will start the search from the 12th index of the base string.   

This is an optional parameter. In case you do not specify the starting-index value then the interpreter will use the default value for the index number which is 0.

endIDX – The third parameter is the end index. It is also an integer parameter. The end-index parameter indicates the index number where the interpreter should stop the search. For example, if you specify 24 as the endIDX value then the interpreter will stop the search as soon as it reaches the index number 24 in the base string.

The endIDX is again an optional parameter. If you want, you can skip it. In case you decide to do so then the interpreter will use the default value for it. The default value is the last index number of your string. Which means in case you skip the endIDX value then the interpreter will keep searching until the base string ends.

Examples of ‘Endswith’ string method.

In order to perform a search, the string method requires a base string. The base string will be the one which will get searched by the interpreter. This could either be a single line string or a multiline string. This could also be a string fetched from the outer source such as a file or a database. For the demonstration, we will use a single-line string. We will discuss other sources such as a database or an external file in another tutorial.

Base String

The base string for the demonstration will be –

“With RebellionRider.com, I will be the master of Python in No Time”

The total length of this base string is 66 characters including white spaces. I will be using this string in upcoming examples.

In order to make the concept clear I will show you three examples using each of the parameters of the ‘endswith’ string method which we discussed above.

Example 1: Endswith string method with Sub-string

In the first example, I will put the ‘endswith’ string method on work and check if the base string ends with the word ‘Time’ or not.

base_str = ‘With RebellionRider.com, I will be the master of Python in No Time’
base_str.endswith (‘Time’) #this will display the result in CLI
 

In the first statement, I created a variable with the name base_str and stored our base string into it. This statement will remain same in all three examples. In the second statement, I call the string method endswith.

As you can see, here we have supplied only one parameter to this method, which is the search string. On execution, the interpreter will start searching the entire string and see if it ends with the specified search string, which in our case is ‘Time’. If the base string ends with the search string then it will return true otherwise it will return false.

I want you to go ahead copy the code and execute it. Also, comment the result you get in the comment section down below.

In case you don’t see the result then replace the statement with this one

print(my_str.endswith('Time')) #This will show the result in the editor

How to call a string method of Python Programming?

To call a string method in Python we use dot notation. In that we first need to write the name of the string object (in our case the string object is the variable which is holding our base string) followed by a dot operator (.) and then the name of the function with proper parameters.

Example 2: Endswith string method with startIDX.

Let’s say I don’t want the interpreter to start the search from the beginning of the string, rather I want it to start the search from index number 39. That way we can save time and resources.

When we have to start the search from a specific index in the string then we use the second parameter. Let’s see how to use it.

base_str = ‘With RebellionRider.com, I will be the master of Python in No Time’
base_str.endswith (‘Time’, 39)

Except for the function call, everything is pretty much the same as earlier. This time in the function call of the endswith method we have added the second parameter. Now on execution, instead of starting to search the base string from the beginning the interpreter will start the search from index number 39.

If video helps you in learning faster then watch this video here

Example 3: Endswith string method with StartIDX and EndIDX

Using both the StartIDX and EndIDX parameter you can reduce the search traversal distance and save the time and resources. Let’s see how?

Let’s say this time I want the search to start from index 39 and end at index 55. Also, instead of Time, let’s see if the string ends with the word ‘Python’ or not.

base_str = ‘With RebellionRider.com, I will be the master of Python in No Time’
my_str.endswith('Python',39, 55) 

As you can see this time the function call contains all the three parameters. On execution the interpreter will not search the entire string rather it will commence the search from the index number 39 and end the search at index 55. 

Now go ahead and make your own example and practice the concept. Also, if you still have any doubts then send me a message on my Facebook or on my twitter.

That is the detailed tutorial on string method endswith of Python Programming.

Before you leave, I want you to share this post on your social media with the #PythonWithRebellionRider if you think this article brought some value to you. Thanks

Good luck and God Speed.