Skip to content
Home » Python program to count number of digits in a number

Python program to count number of digits in a number

Learn about Python program to count number of digits in a number in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Python program to count number of digits in a number

While declaring the variable, we will take an input number from the user.
Then, using the while loop, count the number of digits in a number. It repeated the process until the test expression num > 0 was evaluated. Get each digit of the number and add one to the count each time one is obtained. The loop is terminated when the value of num is set to 0.
Finally, print the value of a number’s number of digits.

Program:

# Python program to count number of digits in a number

#input number
num = int(input('Enter any number: '))

# count number of digits
count = 0
while (num>0):
    num = num//10
    count = count+1
    
# printing number of digits
print('Number of digits:', count)

Output:

Enter any number: 745
Number of digits: 3

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Find Average of N Numbers using Function in Python