I just started learning python, and I can think of two ways to count letters in a string (ignoring numbers, punctuation, and white spaces)
- Using for loop:
for c in s:
if c.isalpha():
counter += 1
print(counter)
- Create a list of alphabets and count the length of the list: (It will create an unwanted list)
import re
s = "Nice. To. Meet. You."
letters = re.findall("([a-z]|[A-Z])", s)
counter = len(letters)
print(counter)
Can anyone tell me is there a "pythonic" way to achieve the same result? like single line code or a function called that will return an int which is the answer? Thank you very much.