Descending Numbers with Diagonal * in Python

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

What You’ll Learn

How to print a descending number pattern from 5 to 1, but with a moving asterisk on a diagonal:

  • 5432*
  • 543*1
  • 54*21
  • 5*321
  • *4321

This is a simple demonstration of how a condition inside a nested loop can replace one character per row.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
5432*
543*1
54*21
5*321
*4321
1

Complete Python Program

The inner loop prints j from 5 down to 1. When i == j, it prints * instead of the number.

Python
for i in range(1, 6):
    for j in range(5, 0, -1):
        if i == j:
            print("*", end="")
        else:
            print(j, end="")
    print()

🧠 How It Works

1

Outer loop controls the row

for i in range(1, 6) creates 5 rows and shifts the asterisk position each row.

Row control
2

Inner loop prints 5 down to 1

for j in range(5, 0, -1) prints descending digits for every row.

Printing
3

Replace one position with *

When i == j, print * instead of j. This creates a diagonal as i increases.

Condition
4

New line after each row

print() moves to the next row.

Line break
=

Diagonal asterisk effect

Each row prints 5 characters. For \(n\) rows of width \(n\), runtime is O(n²).

2

Variation — User Input Version

Pick the size at runtime. This prints numbers from rows down to 1 and replaces one position with * on each row.

Python
rows = int(input("Enter the number of rows: "))

for i in range(1, rows + 1):
    for j in range(rows, 0, -1):
        if i == j:
            print("*", end="")
        else:
            print(j, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Use a different replacement character (like # or @)
  • Flip the diagonal direction by changing the match condition
  • Add spaces between numbers for readability
  • Generate the row as a string and print once per row

Avoid

  • Forgetting print() after the inner loop
  • Using inconsistent end values that break the row width
  • Assuming user input is always valid (wrap int() conversion if needed)
  • Changing the range without updating expected output

Key Takeaways

1

Each row prints the same descending sequence from rows to 1.

2

A single if can replace one character per row to create a diagonal.

3

The match condition i == j controls where * appears.

4

This approach generalizes easily to any size triangle/grid.

❓ Frequently Asked Questions

Because the row index i changes each iteration, and the star prints only when i == j. Since j is counting down, the match shifts left each row.
Replace the condition with if i == (rows - j + 1) in the user-input version. That makes the star move in the opposite direction.
Yes. Change the else branch to print(j, end=" "). If you do, also update your expected output to include spaces.
O(n²) for \(n\) rows, since each row prints \(n\) characters.

Explore More Python Number Patterns!

Try more patterns that mix numbers and symbols to strengthen your loop skills.

All Number Patterns →
Did you know?

Many grid-based algorithms (like matrix diagonals) rely on index comparisons similar to i == j. Pattern printing is a fun way to practice that idea.

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