-1

how do I code a program that would produce this below?

As an example, the following code fragment:

print (count_letters("ab1c2d345"))

should produce the output:

4

hoothoot
  • 9
  • 2

2 Answers2

0

You can try this:

def count_letters(s):
   return len([i for i in s if i.isalpha()])

print(count_letters('ab1c2d345'))

Output:

4

Or, you can use regex for a cleaner solution:

import re
def count_letters(s):
   return len(re.findall('[a-zA-Z]', s))
Ajax1234
  • 66,333
  • 7
  • 57
  • 95
0

You can do it using simple loop/if statement.

def count_letters(str_ltr):
    count_ltr = 0
    for ch in str_ltr:
        if ch.isalpha():
            count_ltr += 1
    return count_ltr


print(count_letters("ab1c2d345"))

Output: 4

utengr
  • 3,087
  • 3
  • 24
  • 62