In this tutorial, we’ll learn what Python string endswith() method is and how to properly use it with the help of an easy and detailed Python code examples.
Introduction: Python String endswith() Method
This method is used to return True(its a Boolean value) if the string ends with a specified suffix. Or else it will return False.
Syntax of endswith() Method
string.endswith(suffixValue, startingPoint, endingPoint)
- Python string endswith() method has three parameters.
- First one is the ending value that will be checked.
- Second parameter is optional. It specifies the starting point from where the search will take place.
- Third parameter is also optional and it can be used to specify an ending point where the search should stop.
- This endswith() method returns True if the string ends with specified suffix, or else it returns False.
Example 1: endswith() applied on a Python String
stringVal='This is Python programming' print(stringVal.endswith('programming')) //true print(stringVal.endswith('Programming')) //false because in original string p is small(lowercase) print(stringVal.endswith('ing')) //true print(stringVal.endswith('ing.')) // false because the string doesnot have (.) after ing print(stringVal.endswith('Python')) // false
Output
True False True False False
As you can see that every character should match the ending of specified string in order for endswith() method to return True. Even a whitespace will make it False if its not included in original string but passed to endswith() as an argument.
Example 2: Applying Range using endswith() Method
stringVal='This is Python programming' print(stringVal.endswith('This is Python programming', 1))
Output
False
We’ve used the same string but still Python string endswith() method returns False. Reason is that the search is starting from second item as starting point index is 1(0,1 so second item). As a result, the strings don’t match and False is returned.
We can specify an ending point as well. See below code:
stringVal='This is Python programming' print( stringVal.endswith('This is Python programming', 0, 25) )
Output
False
The ending point comes before the last character of string which will again result in False. In order to make it True, we have to specify the starting and ending point from the very beginning and endling of string if we’ve passed the whole string as an argument to Python string endswith() method. See below code:
stringVal='This is Python programming' print( stringVal.endswith('This is Python programming', 0, 26) )
Output
True
We can also specify a different ending point of string using these starting and ending range parameters. See below code:
stringVal='This is Python programming' print( stringVal.endswith('Python', 0, 14) )
Output
True
Example 3: Using Python tuple() inside endswith() Method
We can use tuple to specify multiple values for endswith() method to check string with. It anyone matches with the ending of specified string then True will be returned, or else False. See below code:
stringVal='This is Python programming' print( stringVal.endswith(('Python', 'work')) ) print(stringVal.endswith(('Python','work','programming','is')))
Output
False // no match found for the ending value of string True // programming match found
Example 4: endswith() Method Return Boolean(True/False) Value
stringVal='This is Python' returnedVal= stringVal.endswith('Python') print(returnedVal)
Output
True
As we can see that either True or False is returned by Python string endswith() method, depending on the result.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string endswith method. I’ll be looking forward to have your valuable feedback on this post. Thank you for reading it.