How to count letters and numbers in a sentence in python?

Question:

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.

Asked By: keyven

||

Answers:

You can use re package as follow:

import re
string = 'Hello 123, hi 43'
n1 = len(''.join(re.findall('[a-zA-Z]+', string)))
n2 = len(''.join(re.findall('[0-9]+', string)))
print(f'The sentence contained {n1} letters and {n2} numbers.')
Answered By: Mohammad Tehrani

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
Answered By: Claudio
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.