Print Diamond Pattern

Print diamond pattern with stars.

Logic BuildingIntermediate
Logic Building
# Take n
n = int(input("Enter number of rows: "))

# Upper half
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    print("*" * (2 * i - 1))

# Lower half
for i in range(n - 1, 0, -1):
    print(" " * (n - i), end="")
    print("*" * (2 * i - 1))

Output

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

Print upper pyramid then inverted lower pyramid.

Key Concepts:

  • Upper: increasing stars
  • Lower: decreasing stars
  • Symmetric pattern