-3

Good day just want to ask on how to solve this activity in python.

Create a python script that accepts a sentence input (string). The script should be able to iterate through each character of the string, and count (int) how many letters, and numbers are there within the string. Once these values are determined the following output should be printed: "The sentence contained n1 letters and n2 numbers." - where n1 is the letter count, and n2 is the number count.

Sample input: Enter sentence: Hello 123*

Sample output: The sentence contained 5 letters and 3 numbers.

keyven
  • 3
  • 1
  • 4
    Please make an honest [attempt](https://idownvotedbecau.se/noattempt/) and then post with any problems with implementation you are having. – rv.kvetch Sep 23 '22 at 03:57
  • 1
    Likely duplicate of many similar questions as this have been asked over many years on this site ... e.g. [How to count digits, letters, spaces for a string in Python?](https://stackoverflow.com/questions/24878174/how-to-count-digits-letters-spaces-for-a-string-in-python) – chickity china chinese chicken Sep 23 '22 at 04:22

1 Answers1

-1

Choose one you like most:

# ----------------------------------------------------------------------
s = "Hello from Claudio 2022-09-23_06:32"
# ----------------------------------------------------------------------
dgts = 0
ltrs = 0
from collections import Counter
for char, cnt in Counter(s).items():
    if   char.isdigit(): dgts+=cnt
    elif char.isalpha(): ltrs+=cnt
print(f'The sentence contained: {ltrs} letters and {dgts} digits')
# ----------------------------------------------------------------------
def ltr(s): return sum(map(str.isalpha,s))
def dgt(s): return sum(map(str.isdigit,s))
print(f'The sentence contained: {ltr(s)} letters and {dgt(s)} digits')
# ----------------------------------------------------------------------
dgts = sum(char.isdigit() for char in s)
ltrs = sum(char.isalpha() for char in s)
print(f'The sentence contained: {ltrs} letters and {dgts} digits')
# ----------------------------------------------------------------------
dgts = 0
ltrs = 0
for char in s:
    if   char.isalpha(): ltrs+=1
    elif char.isdigit(): dgts+=1
print(f'The sentence contained: {ltrs} letters and {dgts} digits')
# ----------------------------------------------------------------------
import re
ltrs = len(''.join(re.findall('[a-zA-Z]+', s)))
dgts = len(''.join(re.findall('[0-9]+',    s)))
print(f'The sentence contained: {ltrs} letters and {dgts} digits')
# -------------------------------------------------------------------
ltrs = []
dgts = []
[ ( ltrs.append(char.isalpha()), dgts.append(char.isdigit()) ) for char in s ]
print(f'The sentence contained: {sum(ltrs)} letters and {sum(dgts)} digits')
# ----------------------------------------------------------------------
ltrs = 0
dgts = 0
def incr(tpl):
    global ltrs, dgts
    is_ltr, is_dgt = tpl
    if is_ltr: ltrs += 1
    if is_dgt: dgts += 1
list(map(incr,[(c.isalpha(),c.isdigit()) for c in s]))
print(f'The sentence contained: {ltrs} letters and {dgts} digits')

giving all the same result:

The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
The sentence contained: 16 letters and 12 digits
Claudio
  • 7,474
  • 3
  • 18
  • 48