On Fri, Apr 30, 2021 at 9:06 AM David Álvarez Lombardi <
alvarezd...@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)
>

If a feature like this is useful -- and I'm not sure it is -- there is a
much better way to do this IMHO. Add a new format converter to the syntax
for replacement fields:

*>>> f"{c for c in dirty if c in string.ascii_letters !j}"*
*'fsjGe'*

where *!j* means join. It could optionally take a separator string as in
this example:

*>>> f"{chr(65 + i) for i in range(4) !j('-')}"*
*'A-B-C-D'*

--- Bruce
_______________________________________________
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/DZL3DAIUSHKAWMVEJMVYLU3AOLYCOHG7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to