[Python-ideas] New explicit methods to trim strings

Chris Angelico rosuav at gmail.com
Tue Apr 2 14:42:19 EDT 2019

On Wed, Apr 3, 2019 at 5:34 AM MRAB <python at mrabarnett.plus.com> wrote:
> The code above contains a subtle bug.
> If suffix == '', then word.endswith(suffix) == True, and
> word[:-len(suffix)] == word[:-0] == ''.
> Each time I see someone do that, I see more evidence in support of
> adding the method.

Either that, or it's evidence that negative indexing is only part of
the story, and we need a real way to express "zero from the end" other
than negative zero. For instance, word[:<0] might mean "zero from the
end", and word[:<1] would be "one from the end". As a syntactic
element rather than an arithmetic one, it would be safe against
accidentally slicing from the front instead of the back.

But that's an idea for another day.


More information about the Python-ideas mailing list