Start learning free

LESSON 213 OF 673 · WHILE LOOPS - CHALLENGES

Number of Digits in Python: Counting with a While Loop

Practice Read the full explanation

Given a non-negative integer n, return how many digits it has.

The digits are the figures from 0 to 9 that n is written with. Dividing a number by 10 without the remainder removes its last digit (542 becomes 54), so you can count how many times that happens until the number is 0. The number 0 itself has one digit.

 

Example 1:

Input: n = 542
Output: 3
Explanation: 542 is written with three digits: 5, 4 and 2.

Example 2:

Input: n = 100
Output: 3
Explanation: The zeros are digits too: 1, 0 and 0.

Example 3:

Input: n = 0
Output: 1
Explanation: 0 is written with one digit.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    PYTHON · WHILE LOOPS - CHALLENGES

    No Of Digits in Python, explained

    Problem overview

    The function noOfDigits receives a non-negative integer n and returns how many digits it is written with. The number 542 has 3 digits and 100 has 3, because zeros count. The number 0 is written with one digit, so the answer for it is 1.

    Approach

    Dividing by 10 with integer division removes the last digit: 542 // 10 is 54, then 5, then 0. The number of divisions needed to reach 0 is the number of digits. So the plan is to divide in a loop and count the turns.

    There is one number the loop can't handle. The loop runs only while n is greater than 0, so for n = 0 it would run zero times and report 0 digits, when the right answer is 1. The function therefore handles 0 first, before the loop.

    Step by step

    1. Return 1 if n is 0. This is the one case the loop would get wrong.
    2. Start count at 0. No digit has been removed yet.
    3. Loop while n > 0. Each turn removes one digit.
    4. Add 1 to count inside the loop: one turn is one digit.
    5. Set n to n // 10. The last digit is dropped, and eventually n is 0 and the loop stops.
    6. Return count after the loop.

    The solution in Python

    class Solution:
        def noOfDigits(self, n: int) -> int:
            if n == 0:
                return 1
            count = 0
            while n > 0:
                count += 1
                n = n // 10
            return count
    

    Details worth knowing:

    • Early return. A return inside the if ends the function at once, so the code after it runs only for numbers other than 0.
    • // keeps the whole part. 542 // 10 is 54. The slash / would give the float 54.2, which never reaches 0 in a useful way.
    • Large values. Python integers don't overflow, so 1000000000 works and returns 10.
    • The string shortcut. len(str(542)) is also 3. The loop shows the arithmetic behind it.

    Why the zero check is needed

    Here is what the loop alone does. Without the first if, a function that only counts turns returns 0 for n = 0 and 1 for n = 5. The second answer is right, the first is not: 0 is written with a digit, even though dividing it by 10 has nothing left to remove. The lesson treats the zero as a special case instead of changing the loop.

    On 542 the loop sees n equal to 542, then 54, then 5, and counts three turns before n becomes 0. The loop runs once for each digit, so the answer is exactly the number of divisions.

    Edge cases and common mistakes

    • Zero. Without the first check the function would return 0 for n = 0.
    • Trailing zeros. 100 goes 100, 10, 1, 0 in three divisions, so it returns 3.
    • Single digits. 7 becomes 0 after one division, so the answer is 1.
    • Counting after the loop. Putting count += 1 outside the loop gives a result of 1 for every positive number.
    • A missing division. If n = n // 10 is left out, n stays positive forever and the program never finishes.

    Testing

    • 542 gives 3.
    • 100 gives 3.
    • 0 gives 1.
    • 7 gives 1.
    • 1000000000 gives 10.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free