This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add str method for removing leading or trailing substrings
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: LambertDW, amaury.forgeotdarc, rhettinger, zhirsch
Priority: normal Keywords: patch

Created on 2008-12-05 00:33 by zhirsch, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
lstrips-67529.patch zhirsch, 2008-12-05 00:33
lstrips-67529.2.patch zhirsch, 2008-12-05 01:44
Messages (6)
msg76953 - (view) Author: Zach Hirsch (zhirsch) Date: 2008-12-05 00:33
I've found that having a way to strip a leading substring from a string
is convienent. I've also gotten bitten by the fact that str.strip takes
a sequence of characters to remove from the beginning -- not a full string.

I've attached a patch that implements lstrips, rstrips, and strips on
str, unicode, and as methods in the string module. I'm not particularly
attached to the names.

Please consider this patch for inclusion in Python. Thanks!
msg76959 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-05 01:17
I cannot say if this new set of function is desirable in python, but I know that 
I already needed this feature sometimes.
It's very easy to write in python code, though:

def rstrips(s, suffix):
    if suffix and s.endswith(suffix):
        s = s[:-len(suffix)]
    return s

About the patch:
- there is a reference leak in string_strips: lstripped must be decref'd
- this both-ends function seems less useful: prefixes are often different from 
msg76966 - (view) Author: Zach Hirsch (zhirsch) Date: 2008-12-05 01:44
Thanks for taking a look.

Yea, it's pretty easy to write it in Python, but I've found that I've
needed it in quite a few things that I've worked on, so I thought it
might be useful in Python itself.

I've updated the patch to fix the reference leak.

I could imagine someone wanting to strip the same string from both
sides, e.g. "-- hello --".strips('--').strip() == "hello". It might also
be a good idea to include str.strips for parallelism with str.strip.
msg76985 - (view) Author: David W. Lambert (LambertDW) Date: 2008-12-05 09:01
Opinion---"Batteries included" doesn't mean "a bewildering variety of 
functions".  Nor does it mean "my programming language has a checklist  
of features" such as I recall the spreadsheet and word processor wars of 
the (19)80's.  Python should indeed make it easy to express your 
algorithms.  It does this through readable syntax, by providing 
container objects of all sorts, with malleable classes.  It gives access 
to operating system and hardware.  Python stays current with softwares 
of the day.  But Python should remain small It's okay to write some 

Must regular expression syntax keep pace with that of perl?  Probably, 
only because the expressions are directly cut and pasteable.  Otherwise, 
the re language has for me exceeded in complexity that which can be had 
more simply with a divide and conquer approach.

Is there a good reason to have the math module duplicate the 
functionality described in math.h?  No.  One needn't know the c language 
exists to program python.  (Historically it was a great and obvious 
first access to math functions in python.  We're years beyond that.  
Hence---generalize hypot.  It's brain dead trapped in 2D!)

Don't accept the challenge for a python vs. (for instance) ruby feature 
checklist.  Python should stick to the sensible.
msg76987 - (view) Author: Zach Hirsch (zhirsch) Date: 2008-12-05 09:19
Sounds good to me, except for one thing: define "sensible".

To me, lstrips seems sensible, since it's a recurring pattern that I've
used in multiple locations. But perhaps my sense of sensibility is warped :)
msg76997 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-12-05 09:54
I agree with Lambert.

Am rejecting this one on the basis that it adds too little value, is too
easily accomplished with pure python, and that it makes the list of
string methods unnecessarily more complex and harder to learn.
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48791
2008-12-05 09:54:48rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg76997
nosy: + rhettinger
2008-12-05 09:19:27zhirschsetmessages: + msg76987
2008-12-05 09:01:50LambertDWsetnosy: + LambertDW
messages: + msg76985
2008-12-05 01:45:03zhirschsetfiles: + lstrips-67529.2.patch
messages: + msg76966
2008-12-05 01:17:52amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg76959
2008-12-05 00:33:33zhirschcreate