Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Print Diamond Pattern

Print a star diamond using two print calls per row. Students trace string repetition, the separate indentation call, and a lower half that skips the widest row.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 8 of 13
print-diamond-pattern.py
1# Take n
2n = int(input("Enter number of rows: "))
3
4# Upper half
5for i in range(1, n + 1):
6 print(" " * (n - i), end="")
7 print("*" * (2 * i - 1))
8
9# Lower half
10for i in range(n - 1, 0, -1):
11 print(" " * (n - i), end="")
12 print("*" * (2 * i - 1))

Output

Enter number of rows: 4
   *
  ***
 *****
*******
 *****
  ***
   *

In this one

  • Repeat strings
  • Split indentation and stars
  • Avoid a doubled center

How it works

  1. 1

    Grow upper-half rows one through n.

  2. 2

    Print padding then the odd-length star string.

  3. 3

    Shrink from n - 1 to one for the lower half.

Edge cases

What happens for n = 1?

What happens for n = 1?

Only the top star prints; the lower loop is empty.

Does n count total diamond rows?

Does n count total diamond rows?

No. It sets the upper-half height and widest half-width.

What's going on

This diamond writes leading spaces first with end empty, then prints the star string on that same row. The top half grows from one star to the widest row.

The bottom begins at n - 1, so the widest row is not repeated. Total height is 2 * n - 1 for positive n. This differs from the joined-string version in how each output row is assembled, not in its intended silhouette.

What happens for n = 1? Only the top star prints; the lower loop is empty.

Does n count total diamond rows? No. It sets the upper-half height and widest half-width.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why start the lower loop at n - 1?

Pick an answer to check it.

Question 2

What happens for n = 1?

Pick an answer to check it.

Questions people ask

What happens for n = 1?

Only the top star prints; the lower loop is empty.

Does n count total diamond rows?

No. It sets the upper-half height and widest half-width.

Why start the lower loop at n - 1?

To avoid duplicating the widest row.