Given that there is very little you can test about a single character, a new construct feels excessive. Basically, the only possible question is "is it in this subset of codepoints?"

However, that use is perfectly covered by the str.translate() method already. Regular expressions also cover this well.

On Fri, Apr 30, 2021, 12:08 PM David Álvarez Lombardi <alvarezdqal@gmail.com> wrote:
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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/