Python Program to Count the Number of Digits Present In a Number – 2023

Hey guys, in this blog we will see a Python Program to Count the Number of Digits Present In a Number.

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

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

num = 70420
count = 0

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

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

Output

Number of digits: 5
  • Here we just keep on shortening our number by dividing it by 10.
  • The flow will be like below:

count = 0 ; num = 70420
count = 1 ; num = 7042
count = 2 ; num = 704
count = 3 ; num = 70
count = 4 ; num = 7
count = 5 ; num = 0

Example 2: Using inbuilt methods

num = 70420
print(len(str(num)))

Output

5

  • Here we are just converting our number to a string using str() and then calculating its length using len().

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 521

Leave a Reply

Your email address will not be published. Required fields are marked *