Python String endswith() With Examples

Python string endswith() function returns True if the string ends with the given suffix, otherwise it returns False.

Python String endswith()

This function syntax is:

The suffix can be a string or a tuple of string suffixes to look for in the string.

The start is an optional argument to specify the index from where test starts.

The end is an optional argument to specify the index where the test has to stop.

Python string endswith() example

Let’s look at a simple example of python string endswith() function.

Let’s look at some examples with the start argument.

The start index is 2 for the first print statement, so the test will use the sub-string “ove Python”. That’s why the first output is True.

For the second print statement, the substring is “hon” that doesn’t end with “Python”. Hence the output is False.

Let’s look at some examples with the start and end arguments.

For the first print statement, the substring is “Python” i.e. ending with “hon” and hence the output is True.

In the second print statement, the substring is “I Love Pyt” and hence the output is True.

For the third print statement, the substring is “thon” that doesn’t end with “Python”, hence the output is False.

Python string endswith() example with Tuple

Let’s look at some examples with Tuple of strings as the suffix.

For the first print statement, string ends with “Python” and hence the output is True.

For the second print statement, string test begins from index position 1 and ends at index position 6 (excluded). So the substring is ” Love” which ends with “Love” and hence the output as True.

python-string-endswith-tuple-suffix

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: