432,385 Members | 847 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Apr 19 '07 #1
4 Replies

 P: n/a 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 Apr 19 '07 #2

 P: n/a On Apr 19, 3:58 pm, Boris DuÅ¡ek

 P: n/a On Apr 20, 6:08 am, Larry Bates Seems like a convenience I've never used. >a="abcdef"a.endswith('cd',2,4) True >a[2:4].endswith('cd') True It's not just a convenience, and not just with endswith. In general: astring[start:end].amethod(anarg) manifests the slice as a separate temporary object, whereas astring.amethod(anarg, start, stop) does not, and should be more efficient when one is looping over a long string Granted endswith's start arg is not wildly useful, but it's orthogonal -- IMHO all string-processing functions should have a pair of start/ end args as a matter of course, unless neither start nor end is useful. Cheers, John Apr 19 '07 #4

 P: n/a On Apr 20, 6:36 am, subscriber123 The function must work whether you pass it foo(str.endswith,"blaahh","ahh"), or foo(str.startswith,"blaahh","aah"). This is a really bad example, but it gets the point across that similar functions must have similar parameters in order to be Pythonic. I personally have never used the second or third parameters in this function nor in str.startswith. Apr 19 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more Python Questions on Bytes 