Count the number of digits of a number with Python in 3 ways:
In this tutorial, we will learn how to count the total number of digits of a number using Python. The program will take the number as an input from the user and print out the result.
I will show you three different ways to calculate the total number of digits in a number.
Method 1: By using a while loop:
With this approach, we will keep removing the rightmost digit of the number until it becomes zero. We will use one counter variable to calculate the total number of digits we need to remove to make it 0. This variable will be the total number of digits of that number.
The program will use the following algorithm:
- Take the user input and store it in a variable.
- Create one counter variable to hold the total number of digits of the number. Initialize this variable as 0 at the start of the program.
- By using a
while
loop, delete the rightmost digit of the number. Assign this new value to the number variable. - Increment the counter variable by 1 on each step. This loop will run until the number becomes zero. At the end of the while loop, this counter variable will hold the required digits count of the number.
- Print the counter variable to the user.
Python program :
The following program uses a while
loop to find the total number of digits of a given number.
count = 0
number = int(input("Enter a number: "))
while number > 0:
number = number // 10
count = count + 1
print(f"Total number of digits : {count}")
You can also download this program on Github.
Explanation:
- The code is similar to the algorithm we have explained above. The variable
count
is the counter variable used to hold the total number of digits in the number. This variable is initialized to zero at the beginning of the program. - We are using the
input()
method to read the user input. This method returns astring
, we are using theint()
method to convert the string to an integer. This value is stored in thenumber
variable. - The
while
loop will run until the value of thenumber
is greater than zero. - Inside the loop, we are dividing the number by 10 and the new value is assigned to the number. If the value is 123, it will be 12 after the division. It is similar to deleting the last digit of the number.
- At the end of each iteration of the loop, we need to increment the value of
count
by 1. Eventually, thenumber
variable will become zero and the loop will stop. - Print the value of the
count
variable at the end of the program.
Example :
Enter a number: 3345
Total number of digits : 4
Method 2: By using a for loop:
We can use a for
loop to count the total digits of a number. We can convert the integer to a string and iterate over the characters of the string one by one. On each step, we need to increment the counter variable by 1 to get the total digits of that number.
The following program uses a for
loop to iterate over the characters of the string value of the given number:
count = 0
number = int(input("Enter a number: "))
for _ in str(number):
count = count + 1
print(f"Total number of digits : {count}")
Download this example on Github
The count
variable holds the total number of digits. If you run this program, it will print similar output.
Enter a number: 1234
Total number of digits : 4
Method 3: By calculating the length of the string:
Instead of using a loop, we can directly convert the integer to a string and get the length of the string. In Python, we can use the len()
method to get the length of a string. To convert a number to a string, we can use the str()
method as shown in the previous example.
The above two examples will fail for negative values. We can use the abs()
method to get the absolute value of a number before we convert it to a string to calculate the length.
For example, if our input value is -123, the abs()
method will convert it to 123. The str()
method will convert it to a string and the len()
method will return the length of the string or 3.
Python program :
The following program uses the above steps to find the number of digits of a number.
count = 0
number = int(input("Enter a number: "))
print(f"Total number of digits: {len(str(abs(number)))}")
You can the above program on Github.
Example :
Similar tutorials :
- Find the number of CPU count using python
- Count the number of words in a file using python
- Count the total number of characters in a string using python
- Count the number of blank spaces in a file using python
- Python program to find LCM of two numbers
- Python program to sort words of a string in alphabetical order