Python Program to Count Number of Digit

This python program calculates number of digit in a given number. For example, 2893 has 4 digit it it, 323 has 3 digit and so on.

In this program augmented assignment operators like n //= 10 is used. Which is simply short form of writing n = n//10 & n//10 indicates Floor or Integer Division.

Python Source Code: Count Digit in Number

# Counting digit in number

# Defining function to count digit
def count_digit(n):
    count = 0
    while n:
        n //= 10
        count += 1

    return count

# Reading number
number = int(input('Enter number: '))

# Displaying result
print('Number of digit in %d is %d.' %(number, count_digit(number)))

Digit Count Python Output

Enter number: 2397
Number of digit in 2397 is 4.