Table of contents
1.
Introduction
2.
Basic Approach to Solve a Star Pattern in Python Using an Example
2.1.
Example: Right Triangle Star Pattern
2.2.
Python
3.
Steps to Solve
3.1.
Set Up the Loop Logic:
4.
Example: Simple Pyramid Pattern
4.1.
Python
5.
Top Star Patterns in Python
5.1.
Square Star Pattern
5.2.
Python
5.3.
Inverted Pyramid Star Pattern
5.4.
Python
5.5.
Pyramid Star Pattern
5.6.
Python
5.7.
Diamond Star Pattern
5.8.
Python
5.9.
Hollow Square Star Pattern
5.10.
Python
5.11.
Hollow Square Star Pattern
5.12.
Python
6.
Frequently Asked Questions
6.1.
Why do we use loops for printing star patterns in Python?
6.2.
How can modifying loop conditions change the pattern output?
6.3.
What is the importance of conditional statements in creating complex patterns?
7.
Conclusion
Last Updated: Apr 24, 2024
Easy

Star Pattern in Python

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Patterns are a fundamental concept in programming that allow you to create visually appealing designs using code. In Python, you can use loops & other basic programming constructs to generate various types of patterns, including star patterns. Star patterns are a popular choice for beginners learning Python because they provide an engaging way to practice basic coding skills while creating something interesting. 

Star Pattern in Python

In this article, we'll talk about different types of star patterns you can create using Python, with proper examples.

Basic Approach to Solve a Star Pattern in Python Using an Example

Creating star patterns in Python mainly involves using loops—for or while. A loop allows us to repeat a set of commands multiple times, which is essential for printing rows of stars. Let's consider a simple example: a right triangle pattern made of stars.

Example: Right Triangle Star Pattern

To create a right triangle star pattern, you would need to understand how the number of stars increases with each new line. Here's a simple breakdown of the process:

  • Determine the number of rows (n) for your pattern. This will also be the maximum number of stars in the last row.
     
  • Use a for loop to iterate from 1 to n. In each iteration, the loop variable (let's call it i) represents the current row number.
     
  • In each iteration of the outer loop, add another for loop to print i stars. This inner loop runs i times.
     
  • Print a newline character (\n) after each row to ensure the next row of stars appears on a new line.
     

Here’s the code to create a right triangle star pattern:

  • Python

Python

n = 5  # Number of rows

for i in range(1, n + 1):  # Loop through each row

   for j in range(i):  # In each row, print 'i' stars

       print('*', end='')  # Print stars, stay in the same line

   print()  # After each row, move to the next line
You can also try this code with Online Python Compiler
Run Code

Output -: 

*
**
***
****
*****

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

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

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

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass