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
   
<https://stackoverflow.com/questions/493819/why-is-it-string-joinlist-instead-of-list-joinstring>
   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 <https://bugs.python.org/issue43900> 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
  alvarezdqal <https://www.linkedin.com/in/alvarezdqal/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MVQGP4GGTIWQRJTSY5S6SDYES6JVOOGK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to