Steven D'Aprano <steve+pyt...@pearwood.info> added the comment:

Here are links to the relevant docs:

https://docs.python.org/3/library/stdtypes.html#str.startswith
https://docs.python.org/3/library/stdtypes.html#str.endswith

Both say:

"With optional *start*, test string beginning at that position. With optional 
*end*, stop comparing string at that position."

That seems perfectly clear to me: if you pass a starting position, the *starts 
with* test (or ends with) considers the substring starting at that position. If 
you pass an ending position, the starts with test considers the substring 
ending at that position.

That makes it equivalent to text[start:end].startswith(prefix) except that no 
copying is done.

It seems that you are reading the start and end positions as somehow requiring 
that the prefix be an exact match for the given slice, e.g. when you ask:

> must it fail because it doesn't match all of text[2:8] ?

and later:

> can only year match because of the start and end?

These questions imply you think that the methods require the specified slice 
*equals* the given affix (prefix/suffix) rather than *start* or *end* with. 
That seems to me to be an unjustified interpretation of what the docs say.

In the absence of any evidence to the contrary, we are surely entitled to 
assume that the *startswith* method remains *startswith* regardless of whether 
a slice (start/end positions) is specified or not. Or to put it another way, it 
goes without saying that specifying a slice (start and/or end positions) 
doesn't change the semantics of the method, it only changes the starting and 
ending positions, precisely as already documented.


Glenn asked:

> text = "Now the day is over"
> text.startswith('the', 2, 8)
> Does it produce True because 'w the' is at the beginning of the text[2:] ?

No, it produces False, because text[2:8] does not start with "the", it starts 
with "w".

> Maybe. Or because there is an ending position, must it fail because it 
> doesn't match all of text[2:8] ?

If fails, but that's not why it fails. If fails because the substring doesn't 
start with the prefix, not because it doesn't equal the prefix.

> text.startswith('day', 8, 10)
> Does this produce True because everything in day matches text[8:10] 

No, it produces False because the substring in the half-open slice 8:10 does 
not start with "day".

> or must it always produce false for any value of text because the
> match is never as long as the prefix string?

Correct, since the slice is only 2 characters long, and the prefix is 3 
characters long, hence the slice can never begin with that prefix.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37490>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to