Author Dennis Sweeney
Recipients Dennis Sweeney
Date 2020-03-11.19:11:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Following discussion here ( ), there is a proposal to add new methods str.cutprefix and str.cutsuffix to alleviate the common misuse of str.lstrip and str.rstrip.

I think sticking with the most basic possible behavior

    def cutprefix(self: str, prefix: str) -> str:
        if self.startswith(prefix):
            return self[len(prefix):]
        # return a copy to work for bytearrays
        return self[:]

    def cutsuffix(self: str, suffix: str) -> str:
        if self.startswith(suffix):
            # handles the "[:-0]" issue
            return self[:len(self)-len(suffix)]
        return self[:]

would be best (refusing to guess in the face of ambiguous multiple arguments). Someone can do, e.g.

    >>> 'foo.tar.gz'.cutsuffix('.gz').cutsuffix('.tar')

to cut off multiple suffixes. More complicated behavior for multiple arguments could be added later, but it would be easy to make a mistake in prematurely generalizing right now.

In bikeshedding method names, I think that avoiding the word "strip" would be nice so users can have a consistent feeling that "'strip' means character sets; 'cut' means substrings".
Date User Action Args
2020-03-11 19:11:38Dennis Sweeneysetrecipients: + Dennis Sweeney
2020-03-11 19:11:38Dennis Sweeneysetmessageid: <>
2020-03-11 19:11:38Dennis Sweeneylinkissue39939 messages
2020-03-11 19:11:38Dennis Sweeneycreate