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

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

Table of Contents

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

Leave a Reply

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