Python endswith() Method

Python Strings


Description

The Python endswith() method is used to determine whether a string ends with a specified suffix. If it ends with the specified suffix, it returns True; otherwise, it returns False. The optional parameters "start" and "end" are the start and end positions for searching the string.

Syntax

Syntax of the endswith() method:

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

Parameters

  • suffix -- This parameter can be a string or an element.
  • start -- The starting position in the string.
  • end -- The ending position in the string.

Return Value

If the string contains the specified suffix, returns True; otherwise, returns False.

Example

The following example demonstrates the endswith() method:

Example (Python 2.0+)

#!/usr/bin/python str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith(suffix); print str.endswith(suffix,20); suffix = "is"; print str.endswith(suffix, 2, 4); print str.endswith(suffix, 2, 6);

The output of the above example is as follows:

True
True
True
False

Python Strings

Other Extensions