0
s = "fff555#$%^"
f =filter(str.isalpha, s)
print("f", f)

I thought I could iterate over s and filter each character in s by str.isalpha function.

I do not want to use regex.

1 Answer 1

4

You want to join after filtering, as filter returns a generator.

s = "fff555#$%^"
f = ''.join(filter(str.isalpha, s))
print("f", f)
Sign up to request clarification or add additional context in comments.

2 Comments

Where would one find documentation regarding "generator". The docs I followed stated: "filter - Construct a list from those element"...
@Paul The behavior of filter changed in Python 3. See the Python 3 docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.