Computer Science, asked by NININP947, 1 year ago

Write a program that accepts a sentence and calculate the number of letters and digits in python

Answers

Answered by Anonymous
3
Here is source code of the Python Program to calculate the number of digits and letters in a string. The program output is also shown below.

string=raw_input("Enter string:") count1=0 count2=0 for i in string: if(i.isdigit()): count1=count1+1 count2=count2+1 print("The number of digits is:") print(count1) print("The number of characters is:") print(count2)

Answered by fiercespartan
5

To check whether they are numbers or digits. we can use .isalpha() and .isdigit() function. Remember that these only function for the strings.

Now, we will first need to assign initial values for the numbers of letters and number of numbers:

numbers = 0

letters = 0

Now, we will define a function to make the code short and very usable.

def let_num(string):

   numbers,letters = 0,0

   for x in string:

       if x.isalpha():

           letters += 1

       elif x.digit():

           numbers += 1

   return letters,numbers

let_num(input('Enter something:'))

Similar questions