[Python-ideas] New explicit methods to trim strings

Chris Angelico rosuav at gmail.com
Sun Mar 31 01:48:36 EDT 2019


On Sun, Mar 31, 2019 at 3:44 PM Steven D'Aprano <steve at pearwood.info> wrote:
> Of course it doesn't help if you come to Python from a language where
> strip() deletes a prefix or suffix, but even if you don't, as I don't,
> there's something about the pattern:
>
>     string = string.lstrip("spam")
>
> which looks like it ought to remove a prefix rather than a set of
> characters. I've fallen for that error myself.

I think it will be far less confusing once there's parallel functions
for prefix/suffix removal. Actually, this is an argument in favour of
matching that pattern; if people see lstrip() and lcut() as well as
rstrip() and rcut(), it's obvious that they are similar methods, and
you can nip over to the docs to check which one you want ("oh right,
that makes sense, cut snips off a word but strip takes off a set of
letters"). But even if it's called cutprefix/cutsuffix (to match
hasprefix/hassuffix... oh wait, I mean startswith/endswith), there's
at least a _somewhat_ better chance that people will grab the right
tool. Plus, it'll be easy to deal with the problems when they come up
- "hey, strip has a weird bug" :: "ah, you want cut instead".

If we're bikeshedding the actual method names, I think it would be
good to have a list of viable options. A quick skim through the thread
gives me these:

* cut_prefix/cut_suffix
* strip_prefix/strip_suffix
* cut_start/cut_end
* Any of the above with the underscore removed
* lcut/rcut
* ltrim/rtrim (and maybe trim)
* truncate (end only, no from-start equivalent)

Of them, I think cutprefix/cutsuffix (no underscore) and lcut/rcut are
the strongest contenders, but that's just my opinion. Have I missed
anyone's favourite spelling? Is there a name that parallels
startswith/endswith?

Regardless of the method name, IMO the functions should accept a tuple
of test strings, as startswith/endwith do. That's a feature that can't
easily be spelled in a one-liner. (Though stacked suffixes shouldn't
all be removed - "asdf.jpg.png".cutsuffix((".jpg", ".png")) should
return "asdf.jpg", not "asdf".)

ChrisA


More information about the Python-ideas mailing list