In this simple python program, we have to count the digits in a number. It's a beginner-level python program.
To understand this example, you should have knowledge of the following Python programming topics:
Wondering How to count the number of digits in the Python program? This basic Python program counts the number of digits in a number, which means if you have a number 789, then we will get the count as 3. Let us look into the python program to understand how this logic is implemented.
Hereafter accepting the number from the user, we start a
while loop till the number is not equal to zero, then we increment a variable for the count by one in each iteration of the while loop in python, and then we divide the number by 10, remove one digit from the number. After all iterations of the
while loop, we print the count variable for printing the result in python language.
STEP 1: Accept the number from the user as a string and convert it to an integer using int in python programming basics.
STEP 2: Initialize a variable for the count and initialize it to zero.
STEP 3: Use a
while loop until the number is not equal to zero.
STEP 4: Increment the count by 1 in each iteration of the while loop in python.
STEP 5: Divide the number by 10 to remove one digit from the number in each iteration.
STEP 6: Print the number of digits as the value of count using print statement in Python programming language.
n=int(input("Enter number:")) count=0 while(n>0): count=count+1 n=n//10 print("The number of digits is:",count)
Enter number : 12345 The number of digits is: 5