I propose a syntax for constructing/filtering strings analogous to the one available for all other builtin iterables. It could look something like this.
>>> dirty = "f8sjGe7"
>>> clean = c"char for char in dirty if char in string.ascii_letters"
>>> clean
'fsjGe'
Currently, the best way to do this (in the general case) seems to be the following.
>>> clean = "".join(char for char in dirty if char in string.ascii_letters)
But I think the proposed syntax would be superior for two main reasons.
- Consistency with the comprehension style for all other iterables (which seems to be one of the most beloved features of python)
- Confusion surrounding the str.join(iter) syntax is very well documented and I believe it is particularly unintuitive when the string is empty
I also believe the following reasons carry some weight.
- Skips unnecessary type switching from str to iter and back to str
- Much much MUCH more readable/intuitive
Please let me know what you all think. It was mentioned (by @rhettinger) in the PBT issue that this will likely require a PEP which I would happily write if there is a positive response.
--
David Álvarez Lombardi
Machine Learning Spanish Linguist
Amazon | Natural Language Understanding
Boston, Massachusetts