endswith() methodThe endswith() method checks for a given suffix in a string.
The Python String endswith() method returns True if a string ends with the specified suffix, and returns False otherwise.
Below is a visual representation of the endswith() method.
string_name.endswith(suffix, start, end)
string_nameis the name of the string.
suffix: Required. This is the string to check.
start: Optional. An integer to indicate where the search should begin.
end: Optional. An integer to indicate where the search should terminate.
The endswith() method returns True if a string ends with the specified suffix, and returns False otherwise.
The following code shows how to use the endswith() method in Python:
Using the endswith() method without start and end arguments.
# Python code that demonstrate# .endswith() methodword = "California"suffix = 'nia'print (word.endswith(suffix))result = word.endswith('for')print (result)result = word.endswith('nia.')print (result)
If no
startandendindexes are specified, the starting and ending indexes are set to and length by default.
Using the endswith() method with start and end arguments.
# Python code to demonstrate# .endswith() methodplatform = "Educative.io"shot = "String endswith() method in python"# start arguement: 8 and start: 17, end: 34 - 1if platform.endswith('io', 8) and shot.endswith('python',17, 34):print ('This shot is available on Educative platform')else:print("shot not found")