Count Number of Digits in a Number Python

We will discuss how to count the number of digits in a number python. We are counting the number of digits using the native method, math module, len() function, and recursive method.

Example of count number of digits in a number:-
Number = 24601
Number of Digits = 5

How to count digits in python

We will take a number while declaring the variable. Then, count the number of digits in a number using the while loop. It iterated until the test expression num > 0 is evaluated. Get each digit of the number and increment the count each time a digit is obtained. The value of num will be 0 and the loop is terminated. Finally, printing the value of the number of digits in a number.

# Python program to count number of digits in a number

# take input
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 for the different input values:-

Enter any number: 54689
Number of digits: 5

Enter any number: 4654979784946
Number of digits: 13

Enter any number: 00684
Number of digits: 3

Length of integer in Python

In this program, we are using len() and str() function to count the number of digits in a number. The str() is to convert the number into a string and the len() is to find the length of the string.

# Python program to count number of digits in a number

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

# count number of digits
count = len(str(num))
    
# printing number of digits
print('Number of digits:', count)

Output:-

Enter any number: 24428050
Number of digits: 8

Python Program to Count Number of Digits in a Number

First, we will be importing the math module. Python’s math module provides log10(logarithm of base 10) to count the number of digits of positive numbers.

# Python program to count number of digits in a number

# importing math module
import math

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

# count number of digits
count = math.floor(math.log10(num)+1)
    
# printing number of digits
print('Number of digits:', count)

Output:-

Enter any number: 04641105101
Number of digits: 10

Number of Digits in a Number in Python using Recursion

We can also use the recursion technique to count the number of digits in a number. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily.

# Python program to count number of digits in a number

# Function for count number of digits
count = 0
def count_Digits(num):
    global count
    if(num > 0):
        count = count + 1
        count_Digits(num // 10)
    return count

# take input
num = int(input('Enter any number: '))
    
# printing number of digits
print('Number of digits:', count_Digits(num))

Output:-

Enter any number: 124647
Number of digits: 6

Also See:- Sum of Digits of a Number in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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