# Python Program to Count the Number of Digits Present In a Number

To understand this example, you should have the knowledge of the following Python programming topics:

## Example 1: Count Number of Digits in an Integer using while loop

``````num = 3452
count = 0

while num != 0:
num //= 10
count += 1

print("Number of digits: " + str(count))``````

Output

`Number of digits: 4`

In this program, the while loop is iterated until the test expression `num != 0` is evaluated to 0 (false).

1. After the first iteration, `num` will be divided by 10 and its value will be 345. Then, the `count` is incremented to 1.
2. After the second iteration, the value of `num` will be 34 and the `count` is incremented to 2.
3. After the third iteration, the value of `num` will be 3 and the `count` is incremented to 3.
4. After the fourth iteration, the value of `num` will be 0 and the `count` is incremented to 4.
5. Then the test expression is evaluated to false and the loop terminates.

## Example 2: Using inbuilt methods

``````num = 123456
print(len(str(num)))``````

Output

`6`

In the above example, we first convert the integer value into string by using str(). Then, we find the length of the string using len().

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to count the number of digits in a number.

• For example, for input `12345`, the output should be 5.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

• Interactive Courses
• Certificates
• AI Help
• 2000+ Challenges