i'm trying to keep only the letters in a string. i am trying to do something like this:
s = '1208uds9f8sdf978qh39h9i#H(&#*H(&H97dgh'
s_ = lambda: letter if letter.isalpha(), s
this errors out. how would a working version look?
i'm trying to keep only the letters in a string. i am trying to do something like this:
s = '1208uds9f8sdf978qh39h9i#H(&#*H(&H97dgh'
s_ = lambda: letter if letter.isalpha(), s
this errors out. how would a working version look?
This is kind of the long way round, but will let you create a filter for any arbitrary set of characters.
how about
or
Alternately:
You don't need a lambda function:
If you really want to use a lambda function you could write it as follows:
But this can also be simplified to:
You probably want a list comprehension here:
However, this will give you a list of strings (each one character long). To convert this into a single string, you can use
join
:If you want, you can combine the two into a single statement:
If you particularly want or need to use a lambda function, you can use
filter
instead of the generator:One handy way to manipulate strings is using a generator function and the
join
method: