Count Letter And Digits In Python

Count Letter And Digits In Python

Published on September 24 2021

In this tutorial, you will learn a python program to count the number of Digits and Letters in a string. We will do so by using the inbuilt python isdigit() and isalpha() method and also learn how python returns multiple outputs from the function.

Source code

string = input("Enter a string : ")

def countLetterAndDigits(string):
    lcount = dcount = 0
    for c in string:
        if c.isdigit():
            dcount+=1
        elif c.isalpha():
            lcount+=1
    return lcount, dcount

letters, digits = countLetterAndDigits(string)

print("No of letters/alphabets : ", letters)
print("No of digits : ", digits)

Video