Diamond Pattern in Python: Two Programs Explained
Two Python programs to print full diamond and half-diamond star patterns, with the loop logic and space-count formula explained for placement prep.
Printing a diamond pattern in Python uses two loops: an outer loop over row numbers and an inner computation that determines how many spaces and stars each row gets.
What the Pattern Looks Like
A full diamond has two symmetric halves. For n=5, the expected output is:
*
***
*****
*******
*********
*******
*****
***
*
The top half has 5 rows (rows 1 through 5) and the bottom half has 4 rows (rows 4 down to 1), giving 9 total rows. For any n, the total row count is 2*n - 1.
A right half-diamond is the same shape without the leading spaces. For n=4:
*
* *
* * *
* * * *
* * *
* *
*
Each row prints stars separated by spaces, building a right-angled triangle upward then mirroring downward. Total rows for n=4: 2*4 - 1 = 7.
Full Diamond Pattern: Code and Formula
The key insight is that every row’s content follows a formula. For row i (counting from 1):
- Leading spaces:
n - i - Stars:
2*i - 1
At row 1 for n=5: 4 spaces, 1 star. At row 5: 0 spaces, 9 stars. The bottom half runs the same formula in reverse, from row n-1 down to row 1.
String multiplication handles both counts without an inner loop:
n = int(input("Enter the number of rows: "))
# Top half (includes center row)
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
# Bottom half (mirror of top, without center row)
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
Row breakdown for n=5:
- Row i=1: 4 spaces, 1 star
- Row i=2: 3 spaces, 3 stars
- Row i=3: 2 spaces, 5 stars
- Row i=4: 1 space, 7 stars
- Row i=5: 0 spaces, 9 stars (center row)
- Row i=4 again: 1 space, 7 stars (bottom half begins)
- Row i=3 again: 2 spaces, 5 stars
- Row i=2 again: 3 spaces, 3 stars
- Row i=1 again: 4 spaces, 1 star
The bottom-half loop uses range(n - 1, 0, -1). The Python docs for range() explain the three-argument form: range(start, stop, step) generates values starting at start, stopping before stop, stepping by step. Starting at n-1 instead of n avoids duplicating the center row. The stop value of 0 means the loop stops after i=1, printing one star on the last line.
Right Half-Diamond Pattern: Code and Logic
The half-diamond removes the leading-spaces logic entirely. Each row i prints i stars separated by spaces:
n = int(input("Enter the number of rows: "))
# Top half
for i in range(1, n + 1):
print(("* " * i).rstrip())
# Bottom half
for i in range(n - 1, 0, -1):
print(("* " * i).rstrip())
The .rstrip() call trims the trailing space that "* " * i produces. For n=4:
- Top half:
*,* *,* * *,* * * * - Bottom half:
* * *,* *,*
The same range(n - 1, 0, -1) pattern for the bottom half applies here for the same reason: stopping before 0 means i=1 is the last row. The Python for-loop tutorial covers this reverse-step iteration in detail.
Common Loop Errors
Three mistakes appear in most incorrect submissions:
- Off-by-one in the top-half range: writing
range(0, n)instead ofrange(1, n + 1)shifts all i values down by 1. Row 0 produces n spaces and2*0 - 1 = -1stars, which Python silently treats as zero stars. Every row is wrong. - Duplicate center row: using
range(n, 0, -1)in the bottom-half loop starts at i=n, which prints the same 9-star center row a second time. The fix isrange(n - 1, 0, -1). - Empty last line: if the bottom loop runs down to i=0, the expression
"* " * 0evaluates to an empty string, but theprint()call still fires and outputs a blank line. The correct stop value keeps i at a minimum of 1.
These three errors are exactly what placement test auto-graders check. The arithmetic is usually right; the loop bounds are where points are dropped. The same kind of off-by-one analysis appears in the Armstrong number check, where the loop range determines which digits get processed. For more programs that reinforce this indexed-iteration pattern, the collection of Python practice programs covers similar ground.
Why Pattern Questions Appear in Placement Tests
Wipro, TCS, Cognizant, and similar companies include pattern-printing questions in their online assessments to check one specific skill: can the candidate translate a visual output structure into a correct loop formula? A candidate who memorises the diamond code will fail when given a hollow square, a right-arrow, or an X pattern. A candidate who understands that row i gets n-i spaces and 2*i - 1 stars can derive any variant.
The formula traces directly from the output structure. At row i, the total print width is always 2*n - 1 characters. Spaces fill the left margin (n - i of them) and stars fill the center (2*i - 1 of them). The sum is (n - i) + (2*i - 1) = n + i - 1, which equals 2*n - 1 only at i=n. This check, deriving counts from position, is what companies are testing.
The same indexed-iteration thinking appears in a different form when building structured outputs in software: generating formatted tables, constructing nested data from flat indices, or assembling LLM prompt templates that vary content by position. If you want to apply that logic to actual LLM calls, TinkerLLM is a Python-friendly sandbox to experiment with at ₹299.
Primary sources
Frequently asked questions
How many rows does a diamond pattern have for n input?
A full diamond for n rows has 2n-1 total rows: n rows in the top half (including the center row) and n-1 rows in the bottom half.
What is the formula for spaces and stars per row in a diamond?
For row i counting from 1, the number of leading spaces is n-i and the number of stars is 2*i-1, where n is the total rows input.
How do I avoid a trailing empty line in the bottom half?
Use range(n-1, 0, -1) for the bottom-half loop. Starting from n-1 means i=1 is the last row, printing one star cleanly with no zero-star empty row.
Can I print a diamond pattern without nested loops?
Yes. String multiplication lets you handle each row in one expression with a single outer loop: print(' ' * (n-i) + '*' * (2*i-1)) covers spaces and stars in one line.
What is the difference between a full diamond and a half-diamond pattern?
A full diamond is symmetric left-to-right, with leading spaces centering each row. A half-diamond removes the leading spaces, so each row starts at the left margin.
Why do placement tests use pattern printing questions?
Pattern questions test whether a candidate can translate a visual shape into a loop formula. Recruiters use them to check indexed iteration and off-by-one error awareness, not memorised code.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)