endswith() method - str class

endswith() method of the str class in Python checks whether a string ends with one of the specified suffixes and return True or False based on the test

Str Updated 10 Mar 2017 1 min read

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

python
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

Output
Does the sample text has the suffix:True

Does the sample text has one of the suffixes:True