[Python-ideas] New explicit methods to trim strings

Mikhail V mikhailwas at gmail.com
Tue Apr 2 11:42:39 EDT 2019

On Sun, Mar 31, 2019 at 3:28 AM Brandt Bucher <brandtbucher at gmail.com> wrote:
> An idea worth considering: one can think of the “strip” family of methods
> as currently taking an iterable of strings as an argument (since a string
> is itself an sequence of strings):
> It would not be a huge logical leap to allow them to take any iterable.
> Backward compatible, no new methods:
> >>> fname.rstrip(('.jpg', '.png', '.gif'))
> It even, in my opinion, can clarify "classic" strip/rstrip/lstrip usage:
> >>> "abcd".rstrip(("d", "c"))
> 'ab'
> Maybe I’m missing a breaking case though, or this isn’t as clear for others. Thoughts?

Now with this syntax I would have to write:

    string.rstrip ([substring])

Which means I would have to remember to put extra brackets.
Not that bad, but needs extra caution, since it would release the type
of the argument and IMO in the end may become confusing.
So you change the behavior by the type of the argument variable.

And again - having an iterable with *multiple* elements
needs useful and transparent behavior first.

The only intuitive behavior seems to be the algorithm:
- take first element
- check if it's  the string suffix
- if yes, cut it off and STOP the iteration
- if not, repeat check with next element

So it guarantees that only one (first) found element will be cut off.
I don't see other useful cases yet. Even this one seems to be odd.
More complicated behavior would  be just too hard to follow.

IMO having separate method is more user-friendly. As for the name, I
like "rcut", "lcut"
Self-explanative enough and matches other similar existing methods names.

More information about the Python-ideas mailing list