Centered Incrementing Number Triangle in Python

What You’ll Learn
How to print a centered number triangle with consecutive integers in Python:
12 3 45 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:
1
2 3 4
5 6 7 8 9Complete 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.
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
Initialize a counter
k = 0 starts the counter that will print consecutive integers across all rows.
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.
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.
Move to the next line
print() starts a new row after each line is printed.
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.
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\).
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
Use a counter variable to print consecutive numbers across rows.
Odd row widths (1, 3, 5, ...) naturally form a centered triangle.
Leading spaces are essential for centering the triangle.
Total numbers printed in \(n\) rows is \(n^2\).
❓ Frequently Asked Questions
i, so the numbers appear centered.print(f"{k:2d}", end=" ")) to keep the triangle aligned.k to your starting value (for example k = 10) and increment before printing as usual.Explore More Python Number Patterns!
Practice centering, counters, and loop control with more number pattern programs.
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.
12 people found this page helpful
