0
0
PythonProgramBeginner · 2 min read

Python Program to Find Number of Digits in a Number

You can find the number of digits in a number in Python by converting it to a string and using len(str(abs(number))), which counts all digits ignoring the sign.
📋

Examples

Input12345
Output5
Input-9876
Output4
Input0
Output1
🧠

How to Think About It

To find how many digits a number has, think about ignoring any negative sign first, then count each digit one by one. Converting the number to a string makes it easy to count characters, which represent digits.
📐

Algorithm

1
Get the input number.
2
Convert the number to its absolute value to ignore any 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 a number: "))
num_digits = len(str(abs(number)))
print("Number of digits:", num_digits)
Output
Enter a number: 12345 Number of digits: 5
🔍

Dry Run

Let's trace the number 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 digits

len('12345') = 5

5

Print result

Output: Number of digits: 5

StepValue
Input number12345
Absolute value12345
String conversion'12345'
Length (digits)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 number to string

Converting to string lets us count each digit easily with len().

Step 3: Count digits

The length of the string is exactly the number of digits in the number.

🔄

Alternative Approaches

Using a loop to count digits
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 uses division to count digits without converting to string, useful if string conversion is not allowed.
Using logarithm
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 method uses math.log10 to find digits but requires handling zero separately.

Complexity: O(d) time, O(d) space

Time Complexity

Converting the number to a string takes time proportional to the number of digits d, so it is O(d).

Space Complexity

The string conversion uses extra space proportional to the number of digits, so O(d).

Which Approach is Fastest?

The string method is simple and fast for most uses. The loop method uses constant space but may be slower for very large numbers. The logarithm method is efficient but requires math functions and special zero handling.

ApproachTimeSpaceBest For
String conversionO(d)O(d)Simplicity and readability
Loop divisionO(d)O(1)No string conversion allowed
LogarithmO(1)O(1)Mathematical approach, large numbers
💡
Use len(str(abs(number))) for a quick and simple digit count.
⚠️
Counting the negative sign as a digit by not using abs() first.