Count the number of numbers in a number using Python. We learn how to count the total number of digits in a number using python in this tutorial. The program receives the number and prints the total number of digits in the given number. We’ll show you three ways to calculate total numbers in a number.
Examples:
Example1:
Input:
number = 27482
Output:
The total digits present in the given number= 5
Example2:
Input:
number = 327
Output:
The total digits present in the given number= 3
- Python Program to Form an Integer that has the Number of Digits at Ten’s Place and the Least Significant Digit of the Entered Integer at One’s Place
- Python Program to Calculate the Number of Digits and Letters in a String
- Python Program to Calculate the Average of a Number’s digits of Every Number in Given List
Program to Count the Number of Digits Present In a Number in Python
There are several ways to count the number of digits present in a number some of them are:
- Using while loop
- Using string length function
- Converting number to list and calculating length of list
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1:Using while loop
The idea behind this approach is to keep eliminating the number’s rightmost digit one by one until the number reaches zero. The following algorithm will be used for this approach:
Approach:
- Scan the input or give static input and store it in number variable
- Make one counter variable to keep track of the entire number count. At the start of the program, set this variable to zero.
- Using a while loop, eliminate the number’s rightmost digit or convert the number to this new number. For instance, if the number is 782, convert it to 78, then to 7, and lastly to 0.
- On each conversion, increment the counter variable by one. Continue until the number reaches zero. This counter variable will hold the total digit count of the number at the end of the while loop.
- Print the counter variable.
Below is the implementation:
# given number given_number = 27482 # initializing a variable that counts the digit of the given number. # initlaize it with 0 digits_count = 0 # using while loop to traverse digit by digit of the given number while (given_number > 0): # divide the number by 10 given_number = given_number//10 # increment the count digits_count = digits_count + 1 # printing the digits count of the given number print("The total digits present in the given number=", digits_count)
Output:
The total digits present in the given number= 5
Explanation:
- The while loop is iterated in this program until the test expression given_num!= 0 is evaluated to 0 (false).
- After the first repetition, given_num will be divided by 10 and will have the value 2748. The count is then increased to 1.
- The result of given_num after the second iteration is 274, and the count is increased to 2.
- The value of given_num will be 27 after the third iteration, and the count will be 3.
- The value of given_num will be 2 after the fourth iteration, and the count will be increased to 4.
- The value of given_num will be 0 after the fourth iteration, and the count will be increased to 5.
- The loop then ends when the condition is false.
Method #2:Using string length function
We begin by converting the number value to a string using str (). The length of the string is then determined using len ().
Below is the implementation:
# given number given_number = 27482 # converting the given_number to string using str() function strnum = str(given_number) # finding the digits of the given_number using len() function digits_count = len(strnum) # printing the digits count of the given number print("The total digits present in the given number=", digits_count)
Output:
The total digits present in the given number= 5
Method #3:Converting number to list and calculating length of list
Approach:
- We first convert the given number to string using str function.
- Then we convert this string to list of digits using list() function.
- Then calculate the length of list using len() function in list.
Below is the implementation:
# given number given_number = 27482 # converting the given_number to list of digits using list() function numberslist = list(str(given_number)) # finding the digits of the given_number using len() function digits_count = len(numberslist) # printing the digits count of the given number print("The total digits present in the given number=", digits_count)
Output:
The total digits present in the given number= 5
This is similar to method #2
Related Programs:
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to count the number of words in a text file
- python program to count the number of lines in a text file
- python program to count the number of blank spaces in a text file
- python program to calculate the number of words and the number of characters present in a string
- python program to calculate the number of digits and letters in a string
- program to clear the rightmost set bit of a number in cpp and python