New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
str.strip() should have a means of adding to the default behaviour #83599
Comments
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 I've not given that any thought so it's probably not the best way of solving the problem. |
Oops, sorry, that should say 'strip' througout not 'split'. My bad. |
This is a new feature, not a bug, so it will apply only to 3.9. All older versions are in feature freeze.
https://docs.python.org/3/library/stdtypes.html#str.isspace https://docs.python.org/3/library/string.html#string.whitespace |
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 |
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. |
Attached is a script I used to test:
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.) |
For this problem, I would reach for regular expressions. They are specifically designed for flexibility and customization. |
In python 3.9 there is also str.removeprefix: |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: