Right-Aligned Descending Numbers Pattern in Python

What You’ll Learn
How to print a right-aligned descending number triangle in Python:
121321432154321
We’ll use spaces to align the triangle and a condition to decide when to print digits.
⭐ Pattern Output
For 5 rows, the pattern looks like this:
1
21
321
4321
54321Complete Python Program
The inner loop runs from 5 down to 1. It prints a space while j is larger than the current row i, and prints j once j <= i.
for i in range(1, 6):
for j in range(5, 0, -1):
if j <= i:
print(j, end="")
else:
print(" ", end="")
print()🧠 How It Works
Outer loop controls rows
for i in range(1, 6) creates 5 rows and sets the largest digit to show in each row.
Inner loop prints fixed-width positions
for j in range(5, 0, -1) always runs 5 times, so each row has a constant width for alignment.
Spaces then digits
If j > i, print a space. Otherwise print j. That makes each row look like spaces followed by i...1.
New line after each row
print() moves to the next line.
Right-aligned triangle
For \(n\) rows of width \(n\), the program prints \(n^2\) positions in total, so runtime is O(n²).
Variation — User Input Version
Choose the number of rows and generate the same right-aligned descending triangle.
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
if j <= i:
print(j, end="")
else:
print(" ", end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Add spaces between digits for readability (then update output)
- Use fixed-width formatting to keep alignment for multi-digit values
- Print the same triangle left-aligned by removing leading spaces logic
- Replace digits with letters to create alphabet triangles
Avoid
- Forgetting
print()after each row - Changing loop bounds without updating expected output
- Mixing default printing with
end=""mid-row - Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
Leading spaces make the triangle right-aligned.
The inner loop prints descending digits, but shows them only when j <= i.
Row \(r\) prints exactly \(r\) digits: \(r, r-1, ..., 1\).
Total printed positions scale as \(n^2\), so runtime is O(n²).
❓ Frequently Asked Questions
j > i. This pads the left side so the digits align to the right.for j in range(i, 0, -1) and remove the space-printing branch.print(j, end=" ") and update your expected output accordingly.Explore More Python Number Patterns!
Practice alignment and loop conditions with more number pattern programs.
Right-aligned patterns are a great way to practice padding and alignment, which are also common in formatting console tables and reports.
12 people found this page helpful
