Python Number Pattern (Remove Last Digit)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A remove-last-digit pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value becomes zero.

Remember
Rule: while num != 0 → print num → num = num // 10

86523
8652
865
86
8     ← start = 86523

In Python use while num != 0, print(num), then num = num // 10 — floor division drops the last digit.

How to Solve It

Use a while loop: print the current value, then divide by 10 with //. Stop when num reaches 0.

MethodIdeaBest for
while + divide by 10Print, then num //= 10 until zeroLearning, interviews, digit drills
Abs for negativesnum = abs(num) before the loopUser input that may be negative

Pseudocode

Pseudocode
while num is not 0:
    print num
    num = num // 10

Cheat sheet

GoalPattern
Loop conditionwhile num != 0:
Print currentprint(num)
Drop last digitnum = num // 10 or num //= 10
Handle negativesnum = abs(num)
Line countEquals the digit count of the starting number

Printing Numbers vs Starting a New Line

APIEffectUse for
print(num, end="")Stays on the same lineRare here — values would glue together
print(num)Prints the value and ends the lineEach step of the pattern

Unlike nested triangle patterns, each iteration needs its own newline — so use bare print(num) every step.

Live Preview

Change the starting number and the digit-removal pattern updates instantly — absolute value, up to 9 digits.

Whole numbers from 1 to 999999999 (negatives use absolute value). Tap a chip or type a value.

Live result num = 86523 · 5 lines
86523
8652
865
86
8

Worked Walkthrough — num = 86523

Trace each print-then-divide step until the loop stops.

StepPrintsAfter // 10
1865238652
28652865
386586
4868
580 (loop ends)

Five digits → five lines → O(d) time. Floor division truncates toward negative infinity for negatives — use abs first for unsigned digit stripping.

Python Programs

Three complete programs: fixed num = 86523, user-input number, and a compact num = 123 demo. Use View Output to reveal sample results.

Example 1 — Fixed num = 86523

Hard-coded start — print, then divide by 10 until zero.

Python
num = 86523

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

How It Works

1. Print first. Show the current num before changing it.

2. Divide by 10. Floor division drops the last digit — 86523 // 10 is 8652.

3. Stop at zero. When num becomes 0, while num != 0 ends.

Example 2 — User Input Number

Read num safely, take absolute value, then run the same loop.

Python
try:
    num = int(input("Enter a number: "))
except ValueError:
    print("Please enter a valid integer.")
    raise SystemExit

num = abs(num)

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

How It Works

1. Prompt and validate. Catch ValueError so bad input does not crash the script.

2. Absolutize. abs(num) keeps the digit-strip logic positive.

3. Same core. Print-then-divide matches Example 1 — only the source of num changes.

Example 3 — Compact num = 123

Three lines only — easy to dry-run on paper before larger demos.

Python
num = 123

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

How It Works

1. Three digits. 123 → 12 → 1 → 0 — three prints, then stop.

2. Trace on paper. Confirm you print before dividing; reverse that order and you miss the first line.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for 86523 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

divide first

Missing the first line

If you divide before printing, the starting number never appears. Always print, then num //= 10.

num = 0

Empty output

while num != 0 skips entirely when the start is already 0 — no lines are printed.

trailing 0

Zeros disappear next step

120 prints 120, then 12, then 1 — the trailing zero is not a separate line after division.

negatives

Sign without abs

Without abs, negative starts print with a minus and still shrink toward zero — use abs when you want unsigned digit stripping.

end=""

All on one line

If you use print(num, end="") instead of print(num), every value glues together on one line.

Bad input

int(input()) throws

Wrap with try/except ValueError before the loop so bad input does not crash.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(d)O(1)
Compact num = 123 (Example 3)O(d)O(1)

Each iteration removes one digit, so the loop runs d times for a d-digit start — linear in digit count, constant extra memory.

Key Takeaways

  • Print then divide: show num, then num //= 10.
  • while until zero: digit count is unknown ahead of time — while num != 0 fits naturally.
  • Integer division: // 10 truncates — that is how the last digit disappears.
  • Next step: Program 61 builds a growing reverse number with modulo.

One line: while num != 0: print(num); num //= 10

Frequently Asked Questions

It prints the starting number on each line while removing the last digit each step. For 86523, output is 86523, 8652, 865, 86, 8.
Integer division by 10 discards the remainder: 86523 // 10 becomes 8652, then 865, then 86, then 8.
The number of steps equals the digit count — unknown until you read the input. while num != 0 keeps going until all digits are stripped.
Program 59 uses nested loops on a grid. Program 60 uses one while loop and integer division on a single number.
Program 60 shrinks the original number by dividing by 10. Program 61 builds a growing reverse number using modulo and division.
A trailing 0 is removed on the next step — 120 becomes 12, then 1.
Apply abs(num) before the loop — see Example 2.
O(d) where d is the number of digits — each iteration removes exactly one digit.

Did you know?

Each iteration prints the current number, then num = num // 10 drops the last digit using integer division — runtime is O(d) for d digits.

Next: Growing Reverse Number Pattern

Continue by building a reverse number digit-by-digit with modulo and division.

Program 61 tutorial →

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.

6 people found this page helpful