Centered Incrementing Number Triangle in Python

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

What You’ll Learn

How to print a centered number triangle with consecutive integers in Python:

  • 1
  • 2 3 4
  • 5 6 7 8 9

This pattern combines alignment spaces and a running counter to print numbers continuously across rows.

⭐ Pattern Output

For 3 rows, the pattern looks like this:

Output
    1 
  2 3 4
5 6 7 8 9
1

Complete Python Program

The outer loop iterates over odd widths (1, 3, 5). The inner loop prints spaces for alignment and increments a counter when printing numbers.

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

🧠 How It Works

1

Initialize a counter

k = 0 starts the counter that will print consecutive integers across all rows.

Setup
2

Outer loop selects odd row widths

for i in range(1, 6, 2) produces 1, 3, and 5—the amount of number slots per row.

Row control
3

Inner loop handles alignment and printing

for j in range(5, 0, -1) counts down to decide when to print spaces (j > i) or numbers. When printing a number, it increments k and prints it.

Alignment
4

Move to the next line

print() starts a new row after each line is printed.

Line break
=

Centered consecutive triangle

The program prints a total of 1+3+5 = 9 numbers, and in general prints \(1+3+...+(2n-1) = n^2\) numbers for \(n\) rows.

2

Variation — User Input Version

Let the user choose the number of rows. This version prints centered rows of widths 1, 3, 5, ..., \(2 \cdot rows - 1\).

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

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

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Print fixed-width numbers (like {k:2d}) for better alignment after 9
  • Replace numbers with characters to build centered alphabet triangles
  • Build each row as a list and join for faster output in large patterns
  • Change the starting number to create variations (e.g., start at 0)

Avoid

  • Forgetting print() after each row
  • Changing the inner loop range without updating alignment logic
  • Assuming user input is always valid (wrap int() conversion if needed)
  • Printing numbers without a trailing space if your expected output includes spacing

Key Takeaways

1

Use a counter variable to print consecutive numbers across rows.

2

Odd row widths (1, 3, 5, ...) naturally form a centered triangle.

3

Leading spaces are essential for centering the triangle.

4

Total numbers printed in \(n\) rows is \(n^2\).

❓ Frequently Asked Questions

Each row prints leading spaces until the printing position reaches the current odd width i, so the numbers appear centered.
Two-digit numbers take more space. Use fixed-width formatting (e.g., print(f"{k:2d}", end=" ")) to keep the triangle aligned.
Yes. Initialize k to your starting value (for example k = 10) and increment before printing as usual.
O(n²) for n rows, since each row processes \(O(n)\) positions and there are \(n\) rows.

Explore More Python Number Patterns!

Practice centering, counters, and loop control with more number pattern programs.

All Number Patterns →
Did you know?

Centered triangles are often generated by looping over a fixed width and deciding whether each position is a space or a value—the same idea used in many ASCII art and console UI layouts.

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