String endswith() in Python

The endswith() method returns True if a string ends with the given suffix otherwise returns False.

Syntax :

str.endswith(suffix, start, end)

Parameters :



suffix : Suffix is nothing but a string which needs to be checked.
start : Starting position from where suffix is needed to be checked within the string.
end : Ending position from where suffix is needed to be checked within the string.

NOTE : If start and end index is not provided then by default it takes 0 and length-1 as starting and ending indexes where ending index is not included in our search.

Returns :

It returns True if string ends with the given
suffix otherwise returns False. 

CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code shows the working of
# .endswith() function
  
text = "geeks for geeks."
  
# returns False
result = text.endswith('for geeks')
print (result)
  
# returns True
result = text.endswith('geeks.')
print (result)
  
# returns True
result = text.endswith('for geeks.')
print (result)
  
# returns True
result = text.endswith('geeks for geeks.')
print (result)

chevron_right


Output :

False
True
True
True

 
CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code shows the working of
# .endswith() function
  
text = "geeks for geeks."
  
# start parameter: 10
result = text.endswith('geeks.', 10)
print(result)
  
# Both start and end is provided
# start: 10, end: 15
# Returns False
result = text.endswith('geeks', 10, 15)
print result
  
# returns True
result = text.endswith('geeks', 10, 14)
print result

chevron_right


Output :

True
True
False


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.