Calculate the number of digits and letters

In this program, we will accept a string and calculate the number of digits and letters.

Steps to solve the program
  1. Take a word as input.
  2. Create two variables to calculate the number of digits and letters and assign their values equal to 0.
  3. Use for loop to iterate over each character from the word.
  4. Check whether the character is a letter or a digit using isaplha() and isnumeric().
  5. Add 1 to the corresponding variable according to the type of the charater.
  6. Print the output.
Solution 1
				
					word = "python1234"
digit = 0
character = 0

for ele in word:
    if ele.isalpha():
        character += 1
    elif ele.isnumeric():
        digit += 1
        
print("Digits :",digit)
print("Characters: ",character)
				
			
Output
				
					Digits : 4
Characters:  6
				
			
Solution 2
				
					inputstr = 'pythons12345'
digits = '0123456789'
letters = ''
# create small case and capital case sequence with ASCII
for i in range(1, 27):
    letters = letters + chr(65+i) + chr(97+i)

digit_count = 0
letter_count = 0
# iterate over each character with loop
for word in inputstr:
    # check if word is digit and increase digit count
    if word in digits:
        digit_count += 1
    # check if word is letters and increase letter count
    elif word in letters:
        letter_count += 1


print("Digit Count :", digit_count)
print("Letter Count :", letter_count)

				
			
Output
				
					Digits Count : 5
Letter Count : 7
				
			

converts all uppercases in the word to lowercase

print the alphabet pattern ‘O’

Leave a Comment