Python program to count the number of digits in a number
In this post, you will learn how to write a Python program to count the number of digits in a number.
During the development process, we may face a situation where we need to count the number of digits in a number. Python provides different ways to count the number of digits in a number.
Python count number of digits using while loop
In the given Python program, we have used the while loop to count the number of digits. First, we have stored the value of the integer in a num variable and also defined a count variable. The while loop is used and the last digit of the number is obtained by using the modulus operator. In each iteration, the count value is incremented if a digit is obtained. This loop terminates when the value of the number is 0.
num = 412523431
count = 0
while num != 0:
num //= 10
count += 1
print("Number of digits in the number are: " + str(count))
Python count number of digits using recursion
In the given Python program, we have used the recursion function to count the number of digits. Recursion function is a function which is called by itself. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.
def countDigit(num):
if num/10 == 0:
return 1
return 1 + countDigit(num // 10)
# Driver Code
num = 42231343122
print("Number of digits in the number are : % d" % (countDigit(num)))
Output of the above code:
Number of digits in the number are : 12
Python count number of digits using math module
In the given example, we have used the math module to count the number of digits in a number.
import math
def countDigit(num):
return math.floor(math.log10(num)+1)
# Driver Code
num = 2521313341275
print("Number of digits in the number are : % d" % (countDigit(num)))
Output of the above code:
Number of digits in the number are : 13
Python count number of digits using in-built method
In the given example, we have used the Python in-built methods len() and str() to count the number of digits.
# Python program to count number of digits
num = 524232
num_digits = len(str(num))
print("Number of digits in the number are : % d" % (num_digits))
Output of the above code:
Number of digits in the number are : 6
Related Articles
Count vowels in a string Python
Python multiline string
Python split strings by comma
Count consonants in a string Python
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module