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: str.strip() should have a means of adding to the default behaviour
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, rhettinger, senji, steven.daprano
Priority: normal Keywords:

Created on 2020-01-22 12:16 by senji, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_ws.py steven.daprano, 2020-01-22 13:50
Messages (9)
msg360459 - (view) Author: Natalie Amery (senji) Date: 2020-01-22 12:16
If I want to remove the default set of 'whitespace' characters plus something else from a string there's currently no way to cleanly specify that.  In addition there's no way to programatically acquire what characters are considered whitespace so you can't call split with an argument constructed of existing whitespace characters with the new things you need.

As an example you could have an additionally= parameter such that:

"   ( 123 )   ".strip() gives "( 123 )" and
"   ( 123 )   ".strip(additionally="()") gives "123"

I've not given that any thought so it's probably not the best way of solving the problem.
msg360460 - (view) Author: Natalie Amery (senji) Date: 2020-01-22 12:23
Oops, sorry, that should say 'strip' througout not 'split'.  My bad.
msg360461 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-01-22 12:29
This is a new feature, not a bug, so it will apply only to 3.9. All older versions are in feature freeze.

> there's no way to programatically acquire what characters are considered whitespace

https://docs.python.org/3/library/stdtypes.html#str.isspace

https://docs.python.org/3/library/string.html#string.whitespace
msg360464 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-01-22 13:14
There's a discrepancy between the string.whitespace constant and actual whitespace as tested by the str.isspace() method.

I've found five ASCII whitespace characters (as reported by isspace) which aren't in the string.whitespace constant, and 18 non-ASCII whitespace characters which aren't in the string.

The good news is that there are no non-whitespace characters in the constant :-)

The non-ASCII ones probably don't matter, as string.whitespace is documented as only being ASCII. But the following are missing:

U+001C, U+001D, U+001E, U+001F, U+0085
msg360466 - (view) Author: Natalie Amery (senji) Date: 2020-01-22 13:28
https://bugs.python.org/issue25433 has a summary of the issue about what actually constitutes whitespace from the perspective of improving the documentation. In terms of what strip does it turns out that string.whitespace is a red herring.

A list of whitespace characters _could_ be produced at runtime using str.isspace() but that would require iterating over the entire space of characters; which is likely to be slow as well as poor style.
msg360467 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-01-22 13:50
Attached is a script I used to test:

* that str.isspace() currently matches the definition given in the docs;

* that string.whitespace contains all whitespace characters, and nothing but whitespace;

* that str.strip() strips off all whitespace characters, and nothing but whitespace;

* that str.split() splits on all whitespace characters, and nothing but whitespace.

I haven't tested the .lstrip rstrip rsplit methods because I'm lazy :-)

The only test that fails is that the string.whitespace is missing some whitespace.

Again, because I'm lazy, I haven't written this as proper unit tests. Besides, it's quite likely all these things are already in the official test suite. (If not, I might re-write the missing parts as unit tests and submit a PR.)
msg360726 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-01-26 09:07
For this problem, I would reach for regular expressions.  They are specifically designed for flexibility and customization.
msg389531 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-03-26 00:22
In python 3.9 there is also str.removeprefix:
https://docs.python.org/3/library/stdtypes.html#str.removeprefix
which you can apply in a loop.
msg389533 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-26 01:16
Marking this as closed because the issue doesn't seem to have gathered much interest.  If someone wants to move this forward, I recommend discussing it on python-ideas.
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83599
2021-03-26 01:16:46rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg389533

stage: resolved
2021-03-26 00:22:32iritkatrielsetnosy: + iritkatriel
messages: + msg389531
components: + Library (Lib)
2020-01-26 09:07:41rhettingersetnosy: + rhettinger
messages: + msg360726
2020-01-22 13:50:08steven.dapranosetfiles: + test_ws.py

messages: + msg360467
2020-01-22 13:28:34senjisetmessages: + msg360466
2020-01-22 13:14:03steven.dapranosetmessages: + msg360464
2020-01-22 12:29:39steven.dapranosetnosy: + steven.daprano

messages: + msg360461
versions: - Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
2020-01-22 12:23:14senjisetmessages: + msg360460
2020-01-22 12:16:56senjicreate