The endswith method

The endswith method checks whether the string ends with the specified substring and returns the values ​​True or False. In the first parameter of the method, we specify the substring we need, in the second and third optional parameters - the index of the beginning and end of the search, respectively.

Syntax

pythonstring.endswith(substring, [search start index], [search end index])

Example

Let's find the substring 'a', specifying the beginning and end to search for:

pythontxt = 'abcadea'
print(txt.endswith('a', 0, 4))

Result of code execution:

pythonTrue

Example

Let's find the substring 'a' by changing the search indices:

pythontxt = 'abcadea'
print(txt.endswith('a', 0, 3))

Result of code execution:

pythonFalse

Example

Now let's check if our substring ends with 'a':

pythontxt = 'abcadea'
print(txt.endswith('a'))

Result of code execution:

pythonTrue

See also

  • method startswith,
    which checks a substring from the beginning of a string
  • method count,
    which returns the number of occurrences of a substring in a string
  • method replace,
    which searches and replaces a substring in a string
  • method find,
    which returns the index of the first match of a substring in a string