The Python endswith() method returns True if the string ends with the specified suffix, otherwise False.
The syntax of the method is: str.endswith(suffix[, start[, end]])
Key Points :
• If no start and end index is defined, the start defaults to 0 and the length is to -1, and no end index is included in our search.
• If the string ends with a suffix used in endswith(), it returns true. Otherwise, it’s going to return false.
• If you want to verify the string ends with one of the list strings, the endswith() string is used in that situation.
• It Returns the Boolean value.
• This method takes tuples; with the help of examples, we’ll teach you.
We’ll be covering the following topics in this tutorial:
Return Value from endswith()
The endswith() method returns a boolean.
• Returns True when the strings end with the suffix defined.
• Returns False if the string does not end with the suffix defined.
endswith() Parameters
The endswith() takes three parameters:
• Suffix: Suffix strings or tuples to be checked.
• start (optional): Start location where the suffix should be checked in the string.
• end (optional): Ending location to search suffix inside the string.
Below is the python program to demonstrate the endswith() function:
# Python program to demonstrate the # use of endswith() function str = "The Best Learning Resource for Online Education"; suffix = "Education"; print (str.endswith(suffix)) print (str.endswith(suffix,20)) suffix = "for"; print (str.endswith(suffix, 1, 30)) print (str.endswith(suffix, 15, 40))
When we run above the program, the outcome is as follows:
True
True
True
False
Python String endswith(): Tuple Sufix
In this case, we check if the string ends with one of the tuple strings. Let’s see the snippet code:
str = "Learning Resource For Online Education" out = str.endswith(('Learning', 'Online')) print(out) out = str.endswith(('Learning', 'Education')) print(out)
When we run above the program, the outcome is as follows:
False
True
Below are several other functions that we can use to work with string in Python 3