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.

classification
Title: Optional argument for string.strip()
Type: enhancement Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: doerwalter Nosy List: brunns, doerwalter, fdrake, gvanrossum, mwh, shaleh
Priority: normal Keywords:

Created on 2001-07-26 08:35 by brunns, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
diff.txt doerwalter, 2002-04-19 14:50
Messages (14)
msg53177 - (view) Author: Simon Brunning (brunns) Date: 2001-07-26 08:35
The .split method on strings splits at whitespace by 
default, but takes an optional argument allowing 
splitting by other strings. The .strip method (and its 
siblings), on the other hand, always strip whitespace. 
On more than one occasion I would have found it useful 
if these methods also took an optional argument 
allowing other strings to be stripped. For example, to 
strip, say, asterisks from a string you could do:

>>>fred = '**word**word**'
>>>fred.strip('*')
word**word

If nothing else, this would meany that we have a good 
answer when people ask about a equivalent to 
Perl's 'chomp' function - string.rstrip(os.linesep).
msg53178 - (view) Author: Sean 'Shaleh' Perry (shaleh) Date: 2002-03-06 22:16
Logged In: YES 
user_id=37132

why is string.replace() not sufficient?
msg53179 - (view) Author: Simon Brunning (brunns) Date: 2002-03-07 08:33
Logged In: YES 
user_id=156912

string.replace doesn't do the same thing. With our 
hypothetical .strip argument, we have:

>>>fred = '**word**word**' 
>>>fred.strip('*') 
word**word

Using replace, we have:

>>>fred = '**word**word**' 
>>>fred.replace('*', '') 
wordword 

See the difference?
msg53180 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2002-03-07 09:24
Logged In: YES 
user_id=6656

FYI, this has already been rejected once as patch #424606.
msg53181 - (view) Author: Simon Brunning (brunns) Date: 2002-03-07 10:45
Logged In: YES 
user_id=156912

Closing, since this has already been rejected by the BDFL.
msg53182 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-04-09 14:31
Logged In: YES 
user_id=6380

Reopened. A good use case is str(2**100).rstrip('L').
msg53183 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-04-09 15:07
Logged In: YES 
user_id=89016

So what happens to patch http://www.python.org/sf/424606
msg53184 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-04-13 01:02
Logged In: YES 
user_id=6380

Oops, I saw your remark too late.

I've checked in my own version of this for regular
strings...

Feel free to check in your version of this for Unicode
strings, and the docs etc.
msg53185 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2002-04-13 02:43
Logged In: YES 
user_id=3066

(I'd say this is a good reason not to use separate trackers
for features, bugs, & patches -- they're too closely related
to keep them separate, and links between them too tedious
when written only in tracker comments!)

I've updated the documentation for these methods in
Doc/lib/libstdtypes.tex revision 1.87.
msg53186 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-04-18 19:28
Logged In: YES 
user_id=89016

I have a patch ready that implements the unicode version, 
and the mixed versions, i.e. str.strip(unicode) and 
unicode.strip(str), but CVS doesn't like me today.

Maybe tomorrow!
msg53187 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-04-19 14:50
Logged In: YES 
user_id=89016

Here is the patch (diff.txt).

This a simple copy of the string version. I had to make the 
unicode version of do_xstrip public (as _PyUnicode_XStrip), 
so that the string version can call it when it gets passed 
a unicode object.
msg53188 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-04-22 16:50
Logged In: YES 
user_id=6380

Walter, please check this in. I haven't reviewed it
thoroughly, but it looks okay, and the functionality is
right.
msg53189 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-04-22 17:44
Logged In: YES 
user_id=89016

OK, checked in as:

Doc/lib/libstring.tex 1.46
Include/unicodeobject.h 2.37
test/string_tests.py 1.15
test/test_unicode.py 1.57
Objects/stringobject.c 2.160
Objects/unicodeobject.c 2.144

Backport will follow.
msg53190 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2002-04-22 18:45
Logged In: YES 
user_id=89016

Backported to the release22-maint branch as:

Doc/lib/libstring.tex 1.45.8.1
Include/unicodeobject.h 2.36.10.1
Lib/test/string_tests.py 1.10.16.3
Lib/test/test_unicode.py 1.47.6.3
Objects/stringobject.c 2.147.6.3
Objects/unicodeobject.c 2.124.6.8
History
Date User Action Args
2022-04-10 16:04:14adminsetgithub: 34836
2001-07-26 08:35:16brunnscreate