Python Program to Count Number of Digits
You can count the number of digits in an integer in Python by converting it to a string and using
len(str(abs(number))) to get the count.Examples
Input12345
Output5
Input-987654321
Output9
Input0
Output1
How to Think About It
To count digits, first ignore if the number is negative by taking its absolute value. Then convert the number to a string so each digit becomes a character. Finally, count how many characters are in that string using length.
Algorithm
1
Get the input number.2
Convert the number to its absolute value to ignore negative sign.3
Convert the absolute number to a string.4
Count the length of the string to find the number of digits.5
Return or print the count.Code
python
number = int(input("Enter an integer: ")) count = len(str(abs(number))) print("Number of digits:", count)
Output
Enter an integer: 12345
Number of digits: 5
Dry Run
Let's trace the input 12345 through the code
1
Input number
number = 12345
2
Absolute value
abs(number) = 12345
3
Convert to string
str(abs(number)) = '12345'
4
Count length
len('12345') = 5
5
Print result
Output: Number of digits: 5
| Step | Value |
|---|---|
| Input number | 12345 |
| Absolute value | 12345 |
| String conversion | '12345' |
| Length count | 5 |
Why This Works
Step 1: Handle negative numbers
Using abs() removes the negative sign so it doesn't count as a digit.
Step 2: Convert to string
Converting the number to a string lets us treat each digit as a character.
Step 3: Count characters
Using len() on the string gives the total number of digits.
Alternative Approaches
Using a loop and integer division
python
number = int(input("Enter an integer: ")) num = abs(number) count = 0 if num == 0: count = 1 else: while num > 0: num //= 10 count += 1 print("Number of digits:", count)
This method counts digits by dividing the number repeatedly by 10. It avoids string conversion but is longer.
Using logarithm
python
import math number = int(input("Enter an integer: ")) num = abs(number) if num == 0: count = 1 else: count = int(math.log10(num)) + 1 print("Number of digits:", count)
This method uses math.log10 to find digit count quickly but requires import and careful zero handling.
Complexity: O(n) time, O(n) space
Time Complexity
Converting the number to a string takes time proportional to the number of digits, so O(n).
Space Complexity
The string conversion uses extra space proportional to the number of digits, so O(n).
Which Approach is Fastest?
The string method is simple and fast for typical use. The loop method is also O(n) but more verbose. The logarithm method is fastest mathematically but needs extra care.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String conversion | O(n) | O(n) | Simplicity and readability |
| Loop with division | O(n) | O(1) | Avoiding string conversion |
| Logarithm method | O(1) | O(1) | Mathematical efficiency with care |
Use
len(str(abs(number))) for a quick and simple digit count in Python.Forgetting to use
abs() causes negative sign to be counted as a digit.