Steps to Solve
When tackling star patterns in Python, following a systematic approach can simplify the process. Here are the general steps you might consider:
-
Define the Pattern's Size: Decide how large the pattern should be. This usually involves determining the number of rows, which will affect all other dimensions of the pattern.
- Choose the Loop Structure: Decide whether to use a for loop or a while loop. For loops are often easier to manage because they are ideal for executing a block of code a specific number of times, which is typical in pattern problems.
Set Up the Loop Logic:
-
Outer Loop: This loop runs once for each row of the pattern. It controls the number of lines your pattern will have.
-
Inner Loop(s): These loops are nested within the outer loop and are responsible for printing the stars (*) and spaces ( ) on each line. The conditions in these loops will vary depending on the pattern's shape (like aligning stars to the left or right).
-
Print Characters: Inside the inner loop(s), use the print() function to output the correct character (star or space). Use end='' in the print() function to avoid moving to a new line after each character.
- New Line After Each Row: After each iteration of the outer loop (each row is completed), print a new line character to move the cursor to the next line.
Example: Simple Pyramid Pattern
Let's apply these steps to create a pyramid pattern where the number of stars increases in the middle of the row:
Python
n = 5 # Total rows
for i in range(1, n + 1): # Loop through each row
# Print leading spaces
for j in range(n - i):
print(' ', end='')
# Print stars
for j in range(2 * i - 1):
print('*', end='')
# Move to the next line after each row
print()

You can also try this code with Online Python Compiler
Run Code
Output:
*
***
*****
*******
*********
Top Star Patterns in Python
Star patterns can range from simple to complex. Here are some of the top star patterns that are frequently practiced by beginners to sharpen their loop handling skills in Python:
Square Star Pattern
This pattern involves creating a square of stars, where the number of stars in each row and column is equal.
Example:
Python
n = 5 # Size of the square
for i in range(n): # Loop for each row
for j in range(n): # Loop for each star in a row
print('*', end=' ')
print() # Move to the next line after each row is printed

You can also try this code with Online Python Compiler
Run Code
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Inverted Pyramid Star Pattern
An inverted pyramid pattern features a series of stars that decrease in number with each subsequent row.
Example:
Python
n = 5 # Number of rows
for i in range(n, 0, -1): # Loop from n to 1
for j in range(i): # Loop to print stars in each row
print('*', end=' ')
print() # New line after each row

You can also try this code with Online Python Compiler
Run Code
Output:
* * * * *
* * * *
* * *
* *
*
Pyramid Star Pattern
The pyramid star pattern consists of rows with an increasing number of stars, centered in each line.
Example:
Python
n = 5 # Number of rows
for i in range(1, n+1): # Loop through each row
# Print spaces
for j in range(n-i):
print(' ', end='')
# Print stars
for j in range(2*i-1):
print('*', end='')
print() # New line after each row

You can also try this code with Online Python Compiler
Run Code
Output:
*
***
*****
*******
*********
Diamond Star Pattern
The diamond pattern combines a pyramid and an inverted pyramid.
Example:
Python
n = 5 # Middle row of the diamond
# Upper half
for i in range(1, n+1):
for j in range(n-i):
print(' ', end='')
for j in range(2*i-1):
print('*', end='')
print()
# Lower half
for i in range(n-1, 0, -1):
for j in range(n-i):
print(' ', end='')
for j in range(2*i-1):
print('*', end='')
print()

You can also try this code with Online Python Compiler
Run Code
Output:
*
***
*****
*******
*********
*******
*****
***
*
Hollow Square Star Pattern
This pattern forms a square outline with stars, keeping the inside of the square empty.
Example:
Python
n = 5 # Size of the square
for i in range(1, n+1):
for j in range(1, n+1):
if i == 1 or i == n or j == 1 or j == n:
print('*', end=' ')
else:
print(' ', end=' ')
print()

You can also try this code with Online Python Compiler
Run Code
Output:
* * * * *
* *
* *
* *
* * * * *
Hollow Square Star Pattern
The hollow square star pattern is an interesting variation of the square pattern where only the border is filled with stars, & the inside remains empty. This pattern is useful for understanding how to control loop execution & make decisions within loops.
Example:
Python
n = 5 # Dimensions of the square
for i in range(1, n + 1): # Loop for each row
for j in range(1, n + 1): # Loop for each column in a row
# Check if current position is on the border
if i == 1 or i == n or j == 1 or j == n:
print('*', end=' ') # Print star for border
else:
print(' ', end=' ') # Print space for inside
print() # New line after each row

You can also try this code with Online Python Compiler
Run Code
Output:
* * * * *
* *
* *
* *
* * * * *
Frequently Asked Questions
Why do we use loops for printing star patterns in Python?
Loops allow us to repeat a set of instructions multiple times, which is essential for creating patterns where the same character needs to be printed several times in a row or column.
How can modifying loop conditions change the pattern output?
Altering loop conditions changes how many times the loop executes, which directly affects the shape & size of the pattern. For instance, increasing the loop range can expand the pattern, while decreasing it makes the pattern smaller.
What is the importance of conditional statements in creating complex patterns?
Conditional statements enable the differentiation between when to print a star or a space, allowing for more intricate designs like hollow patterns where only the borders are filled.
Conclusion
In this article, we have learned about creating basic & complex star patterns in Python. We started with simple right triangle & square patterns & gradually moved to more elaborate designs like pyramids, diamonds, & hollow squares. By understanding these patterns, you not only have an understanding of loops & conditional statements but also enhance your problem-solving skills in Python.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.