[Python-ideas] New explicit methods to trim strings

Cameron Simpson cs at cskk.id.au
Fri Mar 29 22:19:02 EDT 2019

On 30Mar2019 12:37, Steven D'Aprano <steve at pearwood.info> wrote:
>On Fri, Mar 29, 2019 at 04:05:55PM -0700, Christopher Barker wrote:
>> This proposal would provide a minor gain for an even more minor 
>> disruption.
>I don't think that is correct. I think you are underestimating the gain
>and exaggerating the disruption :-)
>Cutting a prefix or suffix from a string is a common task, and there is
>no obvious "battery" in the std lib available for it. And there is a
>long history of people mistaking strip() and friends as that battery.
>The problem is that it seems to work:
>py> "something.zip".rstrip(".zip")
>until it doesn't:
>py> "something.jpg".rstrip(".jpg")

Yeah, this is a very common mistake. I don't think I've made it myself 
(not really sure why, except that I use strip a lot to remove whitespace 
so I don't think about the file extesion thing for it).

But I've seen people make this mistake. And personally I strip prefixes 
or suffixes from strings a lot and the "measure the suffix and get 
s[:-len(suffix)]" shuffle is tedious. Also I need to decode that shuffle 
in my head every time I see it _and_ debug it because in the file 
extension case I'm always concerned as to whether it gets the "." 
separator or not. With .cutsuffix('.foo') it is really obvious and 

Also, I'm curious - how often to people use strip() to strip stuff other 
than whitespace? It is rare or unknown for myself.

So I am a data point for the individually small but common gain.

[...adding a method to str is only going to break quite weird code...]
>The functionality is clear, meets a real need, is backwards compatible,
>and has no significant downsides. The only hard part is bikeshedding
>names for the methods:
>    lcut rcut
>    cutprefix cutsuffix
>    ltrim rtrim
>    prestrip poststrip
>    etc.
>Am I wrong about any of these statements?

I do not think so. I agree with everything you've said, anyway.

For the shed:

I'm a big -1 on ltrim and rtrim because of confusion with the VERY well 
known PHP trim function which does something else.

I like lcut/rcut as succienct and reminiscent of the UNIX "cut" command.

I like cutprefix and cutsuffix even more, as having similar heft as the 
startswith and endswith methods.

I dislike prestrip and poststrip because of their similarity to strip, 
which like the PHP trim does something else. -1 here too.

Cameron Simpson <cs at cskk.id.au>

More information about the Python-ideas mailing list