Significance of "start" parameter to string method "endswith"

Larry Bates larry.bates at websafe.com
Thu Apr 19 16:08:47 EDT 2007


Boris Dušek wrote:
> Hello,
> 
> what is the use-case of parameter "start" in string's "endswith"
> method? Consider the following minimal example:
> 
> a = "testing"
> suffix="ing"
> a.endswith(suffix, 2)
> 
> Significance of "end" is obvious. But not so for "start".
> 
> Let's assume the "end" parameter is not used - then the function
> should simple check that the last "len(suffix)" characters of "a" are
> equal to "ing", no matter where we start (the function does not *scan*
> the string from the "start", does it?)
> Only case where it would make difference is if we had start +
> len(suffix) < len(a)     (excuse possible "of-by-one" error :-)
> Then the function would never return True. But is there a real use
> case when we would test for endswith like this? (knowing that it must
> return false?)
> 
> Thanks for any ideas/experience.
> Boris
> 

Seems like a convenience I've never used.

>>> a="abcdef"
>>> a.endswith('cd',2,4)
True
>>> a[2:4].endswith('cd')
True

-Larry



More information about the Python-list mailing list