Find Number of Digits in a Number in Python

Find Number of Digits in a Number in Python

Muhammad Maisam Abbas Aug-10, 2021 Jun-07, 2021 Python Python Number
  1. Find the Number of Digits Inside a Number With the math.log10() Function in Python
  2. Find the Number of Digits Inside a Number With the len() Function in Python

This tutorial will introduce the methods to count the number of digits inside a number in Python.

Find the Number of Digits Inside a Number With the math.log10() Function in Python

The math.log10() function inside the math module of Python is used to find the log of base 10 for any specified number. As the integers are also in base 10, we can get the number of digits inside the specified integer with this method.

The following code snippet shows us how we can find the number of digits inside a number with the math.log10() function.

import math
n = -10
if n > 0:
    digits = int(math.log10(n))+1
elif n == 0:
    digits = 1
elif n < 0:
    digits = int(math.log10(-n))+2
print(digits)

Output:

3

We calculated the number of digits inside the number -10 with the math.log10() function in the code above. This code also handles the case where the number is 0, as the log of 0 cannot be calculated. We first check whether the number is greater than 0. If the number is greater than 0, we calculate the digits by taking the log and adding 1 to the result. This process is done because the log of any number is 1 less than the number of digits inside that number.

If the number is equal to 0, we set the digits equal to 1. If the number is less than 0, we calculate the number of digits by taking the log of the additive inverse of that negative number and adding 2 to the result. In the case of negative numbers, we add an extra 1 because we consider the - sign as a digit in this example. In the end, we print the number of digits on the screen.

This method is great to use if we want to determine the number of digits inside an integer. However, it doesn’t work with decimal numbers or any numbers with a floating decimal point.

Find the Number of Digits Inside a Number With the len() Function in Python

The len() function is a built-in function in Python used to calculate the number of characters inside a string variable. The len() function takes a string as an input parameter and returns the number of characters inside that string. To calculate the number of digits inside a number using the len() function, we first have to convert that number into a string with the str() function.

The str() function is also a built-in function in Python used to convert objects of different types into a string variable. The following code snippet shows us how we can find the number of digits inside a number with the len() function:

n = -100.90
digits = len(str(n))
print(digits)

Output:

6

We calculated the number of digits inside the number -100.90 with the len() function in the code above. First, we converted the number into a string using the str() function. After that, we passed the resultant string to the len() function and stored the values returned by the len() function inside the digits variable. In the end, we printed the value inside the digits variable. The output clearly shows that this approach is also applicable for decimal numbers or numbers containing a floating decimal point.

The len() method is far superior to the math.log10() method for finding the number of digits inside a decimal number in Python. The reason is the len() method is clear, concise, and also handles floating decimal points unlike the math.log10() method, which is unnecessarily complex and doesn’t handle floating decimal points.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python Number

  • Create a List of Odd Numbers in Python
  • Convert Letter to Number in Python
  • Display a Number With Leading Zeros in Python
  • Check if a Character Is a Number in Python
  • Implement the GCD Operation in Python