Endswith() method - str class

Method Name:

endswith

Method Signature:

endswith(suffix[, start[, end]])

 

Method Overview:

  • The endswith() function of str class in Python checks whether a string ends  with a specified suffix.
  • The method can be passed of a tuple of suffixes instead of just one suffix.
  • The method returns True if the string ends with the specified suffix or one of the specified suffixes; returns False otherwise.
  • The method can be specified of start and stop indexes. If specified, the method will start and stop the search for the suffix from those positions.

Example:

sampleText     = "Shall I compare thee to a summer's day"

endWord        = "day"

endWords       = ("day", "summer's day")

endsWithWord   = sampleText.endswith(endWord)

 

print("Does the sample text has the suffix:{}".format(endsWithWord))

 

endsWithWords   = sampleText.endswith(endWords)

print("Does the sample text has one of the suffixes:{}".format(endsWithWords))

 

Output: 

Does the sample text has the suffix:True

Does the sample text has one of the suffixes:True


Copyright 2024 © pythontic.com