Remove-Last-Digit Number Pattern in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Integer Division

What You’ll Learn

How to print a simple number pattern in Python by repeatedly removing the last digit of a number.

You’ll use a while loop and integer division by 10 to shrink the number one digit at a time.

⭐ Pattern Output

For num = 86523, the pattern looks like this:

Output
86523
8652
865
86
8
1

Complete Python Program

Print the number, then update it with num //= 10 until it becomes 0.

Python
num = 86523

while num != 0:
    print(num)
    num //= 10

🧠 How It Works

1

Start with a number

num = 86523 is the initial value we want to shrink line by line.

Setup
2

Loop until it becomes 0

while num != 0 keeps printing until there are no digits left.

Loop
3

Print the current value

print(num) outputs the current value on its own line.

Output
4

Remove the last digit

num //= 10 removes the last digit by integer division.

Update
=

Shrinking digits

Each iteration removes exactly one digit, so the loop runs once per digit.

2

Variation — User Input Version

Let the user enter a number and print the same remove-last-digit pattern.

Python
num = int(input("Enter a number: "))
num = abs(num)

if num == 0:
    print(0)
else:
    while num != 0:
        print(num)
        num //= 10

💡 Tips for Enhancement

Try These

  • Use a different base (e.g., //= 2) to explore other shrinking patterns
  • Store the values in a list first if you want to reuse them later
  • Print the removed digit each step using num % 10
  • Handle negative numbers by printing the sign separately
  • Format output with prefixes like Step 1: for clarity

Avoid

  • Using floating-point division (use // for integers)
  • Forgetting the num == 0 case (nothing would print)
  • Assuming user input is always valid without handling exceptions (optional)
  • Mixing up / and // (they behave differently)

Key Takeaways

1

Each loop iteration prints the current number and removes one digit.

2

num //= 10 is the key operation to remove the last digit.

3

The loop runs once per digit, so it is O(d).

4

This idea also helps in digit-based problems like reverse and sum of digits.

❓ Frequently Asked Questions

// performs integer division, which removes the remainder and keeps num as an integer.
Use num % 10 to get the last digit before doing num //= 10.
You can take abs(num) (as shown) to treat the digits the same way, or preserve the sign separately.
O(d), where d is the number of digits in the number.

Explore More Python Number Patterns!

Digit-based loops are a great foundation for reverse, sum-of-digits, and palindrome checks.

All Number Patterns →
Did you know?

Integer division by 10 is one of the most common tricks in digit problems: it removes the last digit, while % 10 extracts it.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful