Python String endswith()

The endswith() method returns True if a string ends with the specified suffix. If not, it returns False.

Example


Syntax of String endswith()

The syntax of endswith() is:

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

endswith() Parameters

The endswith() takes three parameters:

  • suffix - String or tuple of suffixes to be checked
  • start (optional) - Beginning position where suffix is to be checked within the string.
  • end (optional) - Ending position where suffix is to be checked within the string.

Return Value from endswith()

The endswith() method returns a boolean.

  • It returns True if a string ends with the specified suffix.
  • It returns False if a string doesn't end with the specified suffix.

Example 1: endswith() Without start and end Parameters

Output

False
True
True

Example 2: endswith() With start and end Parameters

Output

True
False
True

Passing Tuple to endswith()

It's possible to pass a tuple suffix to the endswith() method in Python.

If the string ends with any item of the tuple, endswith() returns True. If not, it returns False


Example 3: endswith() With Tuple Suffix

Output

False
True
True

If you need to check if a string starts with the specified prefix, you can use startswith() method in Python.