Python OOPs


Examples


Others

endswith() method in Python


The encode() method returns true if the string ends with the given string, otherwise returns False.

Syntax
string.endswith(str, start, end)

Parameters

  • str - (Required) The string to check if the string ends with(case-sensitive).
  • start - (Optional) The index to start the search.
  • end - (Optional) The index to end the search.

The following example shows, how works the endswith() method.

Example
txt = "This is sample text."

r = txt.endswith(".") # returns True
print(r)

r = txt.endswith("text.") #returns True   
print(r)

r = txt.endswith("TEXT.") #returns False   
print(r)

r = txt.endswith("text.",5) #returns True   
print(r)

r = txt.endswith("is",0,4) #'This' endswith 'is'? returns True
print(r)

Output

True
True
False
True
True

Python String Methods