Python Program to Count Digits in a Number
You can count digits in a number in Python by converting it to a string and using
len(str(abs(number))) to get the count of digits ignoring the sign.Examples
Input12345
Output5
Input-987654321
Output9
Input0
Output1
How to Think About It
To count digits in a number, think of the number as a sequence of characters. By converting the number to a string, each digit becomes a character. Then, count how many characters are in that string. Ignore any negative sign by taking the absolute value first.
Algorithm
1
Get the input number.2
Convert the number to its absolute value to ignore negative signs.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 a number: ")) digit_count = len(str(abs(number))) print("Number of digits:", digit_count)
Output
Enter a number: 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: Convert to absolute value
Using abs() removes any negative sign so only digits remain.
Step 2: Convert number to string
Converting to string lets us treat the number as characters to count.
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 a number: ")) count = 0 num = abs(number) 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 math.log10
python
import math number = int(input("Enter a number: ")) num = abs(number) if num == 0: count = 1 else: count = int(math.log10(num)) + 1 print("Number of digits:", count)
This uses logarithms to find digit count efficiently but requires importing math and careful handling of zero.
Complexity: O(n) time, O(n) space
Time Complexity
Converting the number to a string takes time proportional to the number of digits, so it is O(n).
Space Complexity
The string conversion requires 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 slower but uses constant space. The math.log10 method is fastest but needs careful zero handling.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String conversion | O(n) | O(n) | Simplicity and readability |
| Loop with division | O(n) | O(1) | Memory efficiency without strings |
| Math log10 | O(1) | O(1) | Performance with numeric math |
Use
len(str(abs(number))) for a quick and simple digit count in Python.Forgetting to take the absolute value causes negative signs to be counted as digits.