Skip to content

S3Q2 · Centered Triangle of Zeroes

⚡ Quick Reference

Type: Full I/O - pattern printing

Core idea: row i (1-indexed) has i zeroes separated by spaces, left-padded with n - i spaces.

n = int(input())
for i in range(1, n + 1):
    row = " ".join(["0"] * i)
    print(" " * (n - i) + row)

Key rules: - Row i has exactly i zeroes - Zeroes are separated by single spaces: "0 0 0" - Leading spaces = n - i (decreases from n-1 to 0) - No trailing space after the last zero - " ".join() handles this automatically


Problem Statement

Problem (I/O type)

Given n, print a centred triangle of n rows where every element is 0. Within each row, zeroes are separated by a single space. No trailing space after the last zero.

Examples:

Input
3
Output
  0
 0 0
0 0 0
Input
4
Output
   0
  0 0
 0 0 0
0 0 0 0

Deriving the pattern

Look at each row carefully for n = 4:

Row i Leading spaces Zeroes Row content
1 3 1 ···0
2 2 2 ··0 0
3 1 3 ·0 0 0
4 0 4 0 0 0 0

(dots represent spaces)

Two formulas:

  • Number of zeroes at row i = i
  • Leading spaces at row i = n - i

Why n - i leading spaces?

The bottom row (i = n) has no leading spaces - it's n zeroes side by side. Each row above it shifts one space to the right, so row i needs n - i leading spaces to centre correctly.


Building a row

The key detail: zeroes are space-separated with no trailing space.

row = " ".join(["0"] * i)
  • ["0"] * i → a list of i zeroes: ["0", "0", "0"]
  • " ".join(...) → joins them with a single space: "0 0 0"

This automatically gives no trailing space - join only puts the separator between elements, never after the last one.

Then prepend the leading spaces:

print(" " * (n - i) + row)

Tracing n = 3

i " " * (n-i) " ".join(["0"]*i) Printed line
1 " " (2 spaces) "0" " 0"
2 " " (1 space) "0 0" " 0 0"
3 "" (0 spaces) "0 0 0" "0 0 0"

Solution approaches

n = int(input())
for i in range(1, n + 1):
    leading_spaces = " " * (n - i)
    zeroes = " ".join(["0"] * i)
    print(leading_spaces + zeroes)

Each part named separately - easy to read and debug.

n = int(input())
for i in range(1, n + 1):
    print(" " * (n - i) + " ".join(["0"] * i))

Collapse into a single print statement. Clean and concise.

n = int(input())
width = 2 * n - 1          # width of the widest row: n zeroes + (n-1) spaces
for i in range(1, n + 1):
    row = " ".join(["0"] * i)
    print(row.center(width))

str.center(width) pads the string equally on both sides to fill width characters. The widest row (i = n) has n zeroes and n-1 spaces = 2n - 1 characters total, so that's the target width.

Trailing spaces

str.center() pads with spaces on both sides - for rows that aren't the widest, this adds trailing spaces as well as leading spaces. The problem says no trailing space, so check whether the grader is strict about this. If it is, use the leading-spaces approach instead.

n = int(input())
width = 2 * n - 1
for i in range(1, n + 1):
    row = " ".join(["0"] * i)
    print(f"{row:>{len(row) + (n - i)}}")

> in a format spec means right-align. len(row) + (n - i) is the total field width: the row's own width plus the leading spaces needed. This prints the row right-aligned into that field - no trailing spaces added.


Common mistakes

Using * instead of join

A common mistake is printing "0" * i which gives "000" instead of "0 0 0". Always use " ".join(["0"] * i) when spaces between elements are required.

Off-by-one in leading spaces

Row 1 needs n - 1 spaces, not n. The formula is n - i, so for i = 1 that's n - 1. Double-check with n = 3: row 1 should have 2 leading spaces - 3 - 1 = 2 .


Key takeaways

01

" ".join(["0"] * i)

The standard way to build a row of i space-separated elements. join never adds a trailing separator - exactly what the problem requires.

02

Leading spaces = n - i

For a bottom-aligned triangle, the number of leading spaces decreases as rows increase. Row i needs n - i leading spaces.

03

Width of widest row = 2n - 1

The bottom row has n zeroes and n - 1 spaces between them = 2n - 1 characters. This is the reference width for str.center() or any width-based alignment.