Python String Method – endswith()

Spread the love

endswith() method –

The endswith method returns True if a string ends with a specified sub string or suffix otherwise returns False.

Syntax –

string.endswith(suffix, start, end)

suffix – the substring or tuple of substrings that needs to be checked

start – the start index from where the search should starts

end – the end index where search ends

Example –

In [1]: string = 'India is a great country'

In [2]: string.endswith('country')
Out[2]: True

In [3]: string.endswith('nation')
Out[3]: False

endswith with start and end parameters –

In [4]: string.endswith('great', 5, 16)
Out[4]: True

In [5]: string.endswith('great', 5, 20)
Out[5]: False

endswith using a tuple of suffixes –

In [7]: string.endswith(('country','nation'))
Out[7]: True

In [8]: string.endswith(('nation','province'))
Out[8]: False

Related Posts –

  1. Python String Method – startswith()

Rating: 1 out of 5.

Leave a Reply