Right-Aligned Descending Numbers Pattern in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a right-aligned descending number triangle in Python:

  • 1
  • 21
  • 321
  • 4321
  • 54321

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:

Output
    1
   21
  321
 4321
54321
1

Complete 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.

Python
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

1

Outer loop controls rows

for i in range(1, 6) creates 5 rows and sets the largest digit to show in each row.

Row control
2

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.

Width
3

Spaces then digits

If j > i, print a space. Otherwise print j. That makes each row look like spaces followed by i...1.

Condition
4

New line after each row

print() moves to the next line.

Line break
=

Right-aligned triangle

For \(n\) rows of width \(n\), the program prints \(n^2\) positions in total, so runtime is O(n²).

2

Variation — User Input Version

Choose the number of rows and generate the same right-aligned descending triangle.

Python
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

1

Leading spaces make the triangle right-aligned.

2

The inner loop prints descending digits, but shows them only when j <= i.

3

Row \(r\) prints exactly \(r\) digits: \(r, r-1, ..., 1\).

4

Total printed positions scale as \(n^2\), so runtime is O(n²).

❓ Frequently Asked Questions

Spaces are printed for values where j > i. This pads the left side so the digits align to the right.
Print only digits using for j in range(i, 0, -1) and remove the space-printing branch.
Yes. Change the print to print(j, end=" ") and update your expected output accordingly.
O(n²) for \(n\) rows, since each row prints \(n\) positions.

Explore More Python Number Patterns!

Practice alignment and loop conditions with more number pattern programs.

All Number Patterns →
Did you know?

Right-aligned patterns are a great way to practice padding and alignment, which are also common in formatting console tables and reports.

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.

12 people found this page helpful