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
string.endswith(substring, [search start index], [search end index])
Example
Let's find the substring 'a', specifying the beginning and end to search for:
txt = 'abcadea'
print(txt.endswith('a', 0, 4))
Result of code execution:
True
Example
Let's find the substring 'a' by changing the search indices:
txt = 'abcadea'
print(txt.endswith('a', 0, 3))
Result of code execution:
False
Example
Now let's check if our substring ends with 'a':
txt = 'abcadea'
print(txt.endswith('a'))
Result of code execution:
True
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