Introduction
Count the Digits in a Number Using Python: Let’s say you have a number—like 12345—and you want to know how many digits are in it.
If you look at it yourself, it’s easy. You just count: 1, 2, 3, 4, 5. That’s 5 digits.
But how do we make Python do that for us?
In this guide, I’ll show you a few ways to count digits in a number using Python. You’ll learn:
- How to count digits by turning the number into a string
- How to count digits without turning it into a string
- and also learn How to use simple math to figure it out
Let’s start with the easiest method.
Count number of digits in a number in Python using len()
and str()
This is one of the easiest ways to count digits in Python. It’s perfect if you’re just getting started.
Let’s say you have a number, like:
94527
You want to know how many digits are in it. Here’s how to do it step by step.
Step 1: Change the number into a string

In Python, we can turn numbers into strings using the str() function. Like this:
str(94527)
This gives you: "94527"
Now, it’s a string with 5 characters.
Step 2: Count how many characters are in the string

To count the characters, we use the len() function:
len(str(94527))
This tells us the answer is 5.
And that’s it! Python has counted the digits for you.
So to repeat:
- First, turn the number into a string using str()
- Then count the characters using len()
You can try it with any number:
len(str(2025)) # This will give you 4
len(str(1000000)) # This will give you 7
This method works well as long as the number is positive.
Count Digits in a Negative Number Using str() and len()
Let’s say you have a number:
n = -12345
If you do this:
len(str(n))
It will return 6, not 5. Why?
Because it’s counting the minus sign too.
To fix that, we can first turn the number positive using abs(). That way, we only count the digits—not the sign.
Here’s the full code:
n = -12345
n = abs(n) # Remove the minus sign
digit_count = len(str(n)) # Count digits
print("Number of digits:", digit_count)
Output:
Number of digits: 5
Easy and clean!
Quick Recap:
- Use abs(n) to ignore the minus sign
- Use str(n) to turn the number into a string
- and we can Use len() to count the digits
This works great for any integer, whether positive or negative.
Must Read
- How to Return Multiple Values from a Function in Python
- Parameter Passing Techniques in Python: A Complete Guide
- A Complete Guide to Python Function Arguments
- How to Create and Use Functions in Python
- Find All Divisors of a Number in Python
Count Digits in a Number Using a While Loop
This time, we’ll use math to break down the number one digit at a time.
Let’s say you have a number like this:
n = 94527
Here’s the idea:
We’ll divide the number by 10 again and again until it becomes 0.
Each time we divide, we remove one digit from the end.
And for every division, we add 1 to our count.

Let me show you the code first:
n = 94527
count = 0
while n > 0:
n = n // 10 # Remove the last digit
count += 1 # Add 1 to the digit count
print("Number of digits:", count)
Let’s walk through it slowly:
- We start with the number
94527
. - We keep dividing it by 10 using
//
. This removes the last digit:- 94527 // 10 → 9452
- 9452 // 10 → 945
- 945 // 10 → 94
- 94 // 10 → 9
- 9 // 10 → 0
- Each time we do this, we add 1 to our counter.
- When the number becomes 0, we stop the loop.
- The counter now holds the number of digits.
In this case, the output will be:
Number of digits: 5
This method works great for:
- Positive integers
- When you want to avoid using str() or len()
Let’s now talk about how to count digits in:
- Negative numbers, like -1234
- Float numbers, like 123.45
We’ll handle each one step by step.
Counting Digits in a Negative Number Using a While Loop

Let’s say you have:
n = -1234
The minus sign isn’t a digit—it’s just telling us the number is negative. So first, we’ll ignore the sign by taking the absolute value using Python’s abs() function.
Here’s the updated code:
n = -1234
n = abs(n) # Make it positive
count = 0
while n > 0:
n = n // 10
count += 1
print("Number of digits:", count)
Output:
Number of digits: 4
So just add n = abs(n)
before your loop, and it’ll work with negative numbers too.
Counting Digits in a Float (like 123.45)

This one’s a little different. A float has digits before and after the decimal point. So you’ll need to decide:
Do you want to count only the digits, or include the decimal point too?
Let’s say you only want to count digits (no decimal point). Here’s how to do it:
Example: 123.45 → Should count as 5 digits
n = 123.45
# Remove the decimal by converting to string
s = str(n)
# Remove the dot
s = s.replace('.', '')
# Count the digits
print("Number of digits:", len(s))
Output:
Number of digits: 5
If you want to use math instead of strings for floats, it’s much trickier and not as reliable due to rounding issues. So it’s best to use the string method for floats.
Bonus: What If You Have a Negative Float?
Let’s say:
n = -78.910
You can use the same idea:
n = abs(n) # Remove the minus sign
s = str(n)
s = s.replace('.', '') # Remove the decimal point
print("Number of digits:", len(s))
Output:
Number of digits: 5
(7, 8, 9, 1, 0)
A Handy Python Function to Count Digits
def count_digits(number):
# Handle negative numbers
number = abs(number)
# Check if the number is a float
if isinstance(number, float):
# Convert to string, remove the decimal point
s = str(number).replace('.', '')
return len(s)
else:
# Integer case: use math (no string conversion)
count = 0
if number == 0:
return 1 # Special case for 0
while number > 0:
number = number // 10
count += 1
return count
Example Usage
print(count_digits(94527)) # Output: 5
print(count_digits(-1234)) # Output: 4
print(count_digits(0)) # Output: 1
print(count_digits(123.45)) # Output: 5
print(count_digits(-78.910)) # Output: 5
This function handles most common cases cleanly and returns the total number of digits—ignoring the minus sign and decimal point.
Now let’s explore how to use logarithms is an efficient way to count digits, especially when dealing with large numbers. The idea behind this method is based on how numbers work in base 10.
Counting Digits Using Logarithms (math.log10)
The logarithm (base 10) of a number tells us how many times the number can be divided by 10 before it reaches 1. This can help us figure out the number of digits in a number.

For example:
- The logarithm of 1000 is 3 because 103=1000, which tells us there are 4 digits in 1000.
- The logarithm of 12345 is approximately 4.1, which tells us that the number has 5 digits.
Let me show you how to implement this method.
Using math.log10 to Count Digits
Step-by-Step:
- Logarithm of a number: log10(n) gives us the power of 10 needed to reach the number.
- Add 1: Since logarithms start counting from zero, we add 1 to get the actual number of digits.
- Handle edge case: The number
0
needs special treatment because the logarithm of 0 is undefined.
Here’s how we can do it:
Python Code:
import math
def count_digits_log(n):
# Handle the edge case where the number is 0
if n == 0:
return 1
# Take the absolute value to ignore the negative sign
n = abs(n)
# Apply the logarithm and add 1 to get the number of digits
return math.floor(math.log10(n)) + 1
Example Usage:
print(count_digits_log(94527)) # Output: 5
print(count_digits_log(-1234)) # Output: 4
print(count_digits_log(0)) # Output: 1
print(count_digits_log(12345)) # Output: 5
print(count_digits_log(1000000)) # Output: 7
How This Works:
- The math.log10(n) function calculates the base-10 logarithm of the number.
- math.floor() rounds down the logarithm value to the nearest integer.
- We add 1 because the number of digits is always one more than the value of the logarithm (since the logarithm is zero-based).
- Special case: 0 is handled separately because log10(0)\log_{10}(0)log10(0) is undefined.
This method is very efficient, especially for large numbers, because it avoids looping through the digits. Instead, it only requires a couple of mathematical operations.
Challenge Practice Section: Test Your Understanding
Ready to put your skills to the test? Try these practice problems to strengthen your understanding of counting digits in Python. These are perfect for learners who want to go beyond the basics.
1. Count Digits in a List of Numbers
Write a function that takes a list of integers and returns a list of digit counts.
Example:
Input:
[123, 0, 99999, -78]
Output:
[3, 1, 5, 2]
Hint: You can use either str(), loops, or math.log10 — your choice!
2. Filter Numbers by Digit Count
Write a program that filters out numbers from a list that don’t have exactly N digits.
Example:
If N = 4 and the list is [23, 1456, 8765, 12, 999], your output should be:
[1456, 8765]
Bonus: Try it without converting numbers to strings.
3. Count Total Digits in a Large List
Write a function that takes a list of numbers and returns the total number of digits across all of them (ignoring minus signs and decimal points).
Example:
Input: [123.45, -67, 8900]
Output: 9 (digits are: 1,2,3,4,5,6,7,8,9 — decimal and minus ignored)
4. Count Digits in Numbers from User Input
Ask the user to enter numbers separated by spaces, then print how many digits each one has.
Example:
Input:
Please enter numbers: 34 -56 7.89 10000
Output:
34 → 2 digits
-56 → 2 digits
7.89 → 3 digits
10000 → 5 digits
Bonus: Add exception handling to skip invalid inputs.
5. Create a Custom Digit Counter Function
Write a function called count_digits_custom that:
- Accepts any type: integer, float, string, list of numbers
- Handles negative numbers and decimals
- Ignores non-numeric characters (like ‘$’, ‘,’, etc. in a string)
Example:
Input: [“1,000”, “$23.45”, “-89”, 300]
Output: [4, 4, 2, 3]
Challenge yourself: Don’t use len(str(x)) directly inside your logic.
🔥 Bonus Challenge: Count Digits Without Any Loops or Strings
Can you count the number of digits in a positive integer using only math and recursion (no while or for loops, and no str())?
Try it for yourself!
Final Thoughts
Counting digits in a number may seem like a small task, but as you’ve seen, there are several smart ways to approach it in Python. Whether you:
- convert the number to a string,
- use loops and division,
- or apply
math.log10
for large numbers,
you now have a solid toolbox of techniques that work for integers, floats, negatives, and even mixed inputs.
This kind of problem strengthens your understanding of Python’s core features—like type conversion, math functions, conditionals, and loops—while also helping you write cleaner and more efficient code.
Tip: Try combining these techniques in creative ways. For instance, how would you count digits in a nested list of numbers? Or sort numbers based on how many digits they have?
Keep Practicing
If you haven’t already, head over to the challenge section above and tackle those exercises. They’re designed to stretch your logic and make sure you truly understand the concepts—not just memorize them.
Got Questions or Solutions to Share?
We’d love to hear from you! Drop your thoughts in the comments or share your code with us on LinkedIn. Teaching others is the best way to master a topic.
Want more Python tricks like this?
Visit us at emitechlogic.com for deep, practical guides that make Python easier to learn and more fun to use.
FAQs
You can use len(str(number)) to count the digits. For example, len(str(12345)) returns 5.
Use a loop and divide the number by 10 repeatedly until it becomes 0. Count how many times you do it. Or use math.log10 for a quicker method.
Yes—if you convert to a string, you’ll need to remove ‘-‘ and ‘.’ before counting. With math or loops, these characters don’t matter.
Yes, but you’ll need to remove the decimal point first. You can do this by converting to a string and using replace(“.”, “”) or multiplying by 10^n if using math.
External Resources
Python’s str() function:
https://docs.python.org/3/library/functions.html
len() function for counting characters:
https://docs.python.org/3/library/functions.html
math.log10() for digit counting:
https://docs.python.org/3/library/math.html
We don’t just show you the code—we let you run it too!
Thanks to embedded tools from Trinket.io, you can practice the examples from this blog post right on the page—no need to install anything.
Leave a Reply