...

Create Stunning Python Star Patterns Using Only 2 For Loops!

Python Star Patterns are a simple programming problem that is used for teaching the concept of loops, logic, and nested loops among the beginners. In this post, we will go over several star patterns in Python by only utilizing two loops: the first one for rows and the second one for columns. These patterns like pyramids, diamonds, right-angled triangles, and more.

Introduction

Many star patterns in coding challenges are the most appropriate for assessing a programmer’s skill to use loops and logic to manipulate data. Nested loops can generate various patterns, but in this article, we will concentrate on the application of a lone with a loop going downward along the rows, and a second loop going to the right along the columns.

Instead of using a traditional nested loop approach, we will optimize python star pattern printing using:

  • String multiplication
  • Loop conditions
  • Row and column-based printing

By using Python’s flexible printing capabilities, we can create beautiful patterns with minimal code.

For Detailed Explaination Watch My Video

Understanding the Two-Loop Concept

The for loop is a frequently used pattern-making tool in python star pattern. By adjusting string multiplication and loop conditions, we can create python star patterns without the need for nested loops.

Why Use Two Loops Instead of Nested Loops?

Usually, when printing star patterns, we use nested loops, where:

  • The outer loop controls the number of rows.
  • The inner loop determines the number of columns (or spaces and stars).

For example, instead of:

for i in range(5): 
    for j in range(5):
        print("*", end=" ")
    print()

We can alse use this approach:

for i in range(5):
    print("* " * 5)

This approach simplifies the code and improves readability for the 5 horizontal lines of python star pattern. For simplified approach, printing star patterns using only one for loop checkout this Top 10 star Patterns in Python.

Simple Star Line Pattern

Code

Printing a line of stars in a single row is the most basic star pattern.

#Prints horizontal python star pattern
for i in range(1):  # Outer loop runs 1 time
    for j in range(5):  # Inner loop runs 5 times
        print("*", end=" ")

Output

* * * * *

Iteration Breakdown

Iteration 1:

i = 0

  • Inner loop runs 5 times, printing * each time.
  • Prints: * * * * *

Since the outer loop runs only once, the result is a single line of stars.

Printing Stars in 5 Lines

Code

for i in range(5):  # Runs 5 times (5 rows)
    for j in range(1):  # Runs once per row
        print("*")

Output

*
*
*
*
*

Iteration Breakdown

Iteration 1:

i = 0

  • Prints: *

Iteration 2:

i = 1

  • Prints: *

Iteration 3:

i = 2

  • Prints: *

Iteration 4:

PM Narendra Modi with text Pradhan Mantri Vaya Vandhana Yojana, promoting senior citizen pension benefits
PMVVY Unlocks ₹9,250/Month Pension & Ultimate Peace

i = 3

  • Prints: *

Iteration 5:

i = 4

  • Prints: *

Each row contains only one star.

Right-Angled Triangle Star Pattern

Code

#Prints right anlged traingle Star Patterns
for i in range(5):  
    for j in range(i + 1):  
        print("*", end=" ")
    print()

Output

Right-angled pyramid python star pattern created using Python loops with increasing stars.

Code Structure

Outer Loop (for i in range(5))

  • Controls the number of rows.
  • Runs from 0 to 4 (total 5 rows).

Inner Loop (for j in range(i + 1))

  • Controls the number of stars in each row.
  • The number of stars printed in each row is i + 1.

print(“*”, end=” “)

  • Prints a * followed by a space (" "), keeping the stars on the same line.

print()

  • Moves to the next line after printing all stars for that row.

Iteration Breakdown

Iteration 1:

i = 0

  • Inner loop runs once (j = 0).
  • Prints: *

Iteration 2:

i = 1

  • Inner loop runs twice (j = 0, 1).
  • Prints: * *

Iteration 3:

i = 2

  • Inner loop runs three times (j = 0, 1, 2).
  • Prints: * * *

Iteration 4:

i = 3

  • Inner loop runs four times (j = 0, 1, 2, 3).
  • Prints: * * * *

Iteration 5:

i = 4

  • Inner loop runs five times (j = 0, 1, 2, 3, 4).
  • Prints: * * * * *

Right-Aligned Triangle Star Pattern

Code

#prints python star pattern of right aligned triangle 
n = 5
for i in range(1, n + 1):  
    for j in range(n - i):
        print(" ", end=" ")
    for j in range(i):
        print("*", end=" ")
    print()

Output

Right-aligned pyramid python star pattern generated using Python loops.

Code Structure

Outer Loop (for i in range(1, n + 1))

  • Controls the number of rows.
  • Runs from 1 to n (i.e., 5 rows).

First Inner Loop (for j in range(n – i))

Udyogini Scheme official banner promoting women entrepreneurship in India
Discover the Udyogini Scheme: 88 Ways to Empower Women Entrepreneurs
  • Prints spaces (" " end=" ") to right-align the stars.
  • The number of spaces decreases as i increases.

Second Inner Loop (for j in range(i))

  • Prints * followed by a space.
  • The number of * increases as i increases.

print()

  • Moves to the next line after printing all stars for that row.

Iteration Breakdown

Iteration 1 (i = 1)

  • Spaces: n - i = 5 - 1 = 4
  • Stars: i = 1

Prints: " *"

Iteration 2 (i = 2)

  • Spaces: n - i = 5 - 2 = 3
  • Stars: i = 2

Prints: " * *"

Iteration 3 (i = 3)

  • Spaces: n - i = 5 - 3 = 2
  • Stars: i = 3

Prints: " * * *"

Iteration 4 (i = 4)

  • Spaces: n - i = 5 - 4 = 1
  • Stars: i = 4

Prints: " * * * *"

Iteration 5 (i = 5)

  • Spaces: n - i = 5 - 5 = 0
  • Stars: i = 5

Prints: "* * * * *"

Inverted Right-Angled Triangle

Code

#prints python star pattern of inverted right angled triangle
for i in range(5, 0, -1):  
    for j in range(i):  
        print("*", end=" ")
    print()

Output

Right-angle reverse triangle python star pattern generated using Python loops.

Code Structure

Outer Loop (for i in range(5, 0, -1))

  • Controls the number of rows.
  • Starts from 5 and decreases to 1, meaning it prints 5 rows.

Inner Loop (for j in range(i))

  • Prints * followed by a space ("*" end=" ").
  • The number of * printed in each row equals the value of i.
  • As i decreases, the number of stars also decreases.

print()

  • Moves to the next line after printing all stars for a row.

Iteration Breakdown

Iteration 1 (i = 5)

  • j runs from 0 to 4 (5 times).

Prints: * * * * *

Iteration 2 (i = 4)

  • j runs from 0 to 3 (4 times).

Prints: * * * *

Iteration 3 (i = 3)

  • j runs from 0 to 2 (3 times).

Prints: * * *

Sack of rice with text highlighting Antyodaya Anna Yojana (AAY)
Antyodaya Anna Yojana (AAY): India’s Lifeline feeding 2.5 Crore Poor Families

Iteration 4 (i = 2)

  • j runs from 0 to 1 (2 times).

Prints: * *

Iteration 5 (i = 1)

  • j runs from 0 to 0 (1 time).

Prints: *

Right-Aligned Triangle Star Pattern

Code

#prints python star pattern of right aligned triangle 
n = 5
for i in range(n): 
    for j in range(i): 
        print(" ", end="")
    for j in range(n - i):  
        print("*", end="")
    print()  

Output

Right-angle reverse star pattern generated in Python with decreasing rows.

Iteration Breakdown

Iteration 1 (i = 0)

  • Spaces: i = 0 → No spaces
  • Stars: n - i = 5 - 0 = 5

Prints:*****"

Iteration 2 (i = 1)

  • Spaces: i = 1 → 1 space
  • Stars: n - i = 5 - 1 = 4

Prints:****"

Iteration 3 (i = 2)

  • Spaces: i = 2 → 2 spaces
  • Stars: n - i = 5 - 2 = 3

Prints:***"

Iteration 4 (i = 3)

  • Spaces: i = 3 → 3 spaces
  • Stars: n - i = 5 - 3 = 2

Prints:**"

Iteration 5 (i = 4)

  • Spaces: i = 4 → 4 spaces
  • Stars: n - i = 5 - 4 = 1

Prints:*"

Pyramid Star Pattern

Code

#prints python star pattern of Pyramid
n = 5
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()

Output

Pyramid star pattern created using Python loops with increasing rows of stars.

Iteration Breakdown

Iteration 1 (i = 1):

  • Spaces printed: n - i = 5 - 1 = 4 spaces
  • Stars printed: 2 * i - 1 = 2 * 1 - 1 = 1 star

Prints: " *"

Iteration 2 (i = 2):

  • Spaces printed: n - i = 5 - 2 = 3 spaces
  • Stars printed: 2 * i - 1 = 2 * 2 - 1 = 3 stars

Prints: " ***"

Iteration 3 (i = 3):

  • Spaces printed: n - i = 5 - 3 = 2 spaces
  • Stars printed: 2 * i - 1 = 2 * 3 - 1 = 5 stars

Prints: " *****"

Iteration 4 (i = 4):

Python Recursion Tutorial with Code Snippets and Visual Aids
Master Recursion Fast: 7 Python Programs That Will Blow Your Mind
  • Spaces printed: n - i = 5 - 4 = 1 space
  • Stars printed: 2 * i - 1 = 2 * 4 - 1 = 7 stars

Prints: " *******"

Iteration 5 (i = 5):

  • Spaces printed: n - i = 5 - 5 = 0 spaces
  • Stars printed: 2 * i - 1 = 2 * 5 - 1 = 9 stars

Prints: "*********"

Pyramid Inverted Star Pattern

Code

#prints python star pattern of pyramid inverted
n=5
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()

Output

Reverse pyramid star pattern generated using Python loops with decreasing rows of stars.

Iteration Breakdown

Iteration 1 (i = 4)

  • Spaces: n - i = 5 - 4 = 1
  • Stars: 2 * i - 1 = 2 * 4 - 1 = 7

Prints: " *******"

Iteration 2 (i = 3)

  • Spaces: n - i = 5 - 3 = 2
  • Stars: 2 * i - 1 = 2 * 3 - 1 = 5

Prints: " *****"

Iteration 3 (i = 2)

  • Spaces: n - i = 5 - 2 = 3
  • Stars: 2 * i - 1 = 2 * 2 - 1 = 3

Prints: " ***"

Iteration 4 (i = 1)

  • Spaces: n - i = 5 - 1 = 4
  • Stars: 2 * i - 1 = 2 * 1 - 1 = 1

Prints: " *"

Diamond Star Pattern

Code

n = 5
# prints python star pattern of Upper pyramid 
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()
# prints python star pattern of Lower reverse pyramid
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()

Output

Diamond star pattern generated using Python loops, showing a symmetrical shape with increasing and decreasing stars.

Iteration Breakdown

Python Star Pattern of Upper Pyramid

Iteration 1 (i = 1)

  • Spaces: n - i = 5 - 1 = 4
  • Stars: 2 * i - 1 = 2 * 1 - 1 = 1

Prints: " *"

Iteration 2 (i = 2)

  • Spaces: n - i = 5 - 2 = 3
  • Stars: 2 * i - 1 = 2 * 2 - 1 = 3

Prints: " ***"

Iteration 3 (i = 3)

  • Spaces: n - i = 5 - 3 = 2
  • Stars: 2 * i - 1 = 2 * 3 - 1 = 5

Prints: " *****"

Iteration 4 (i = 4)

  • Spaces: n - i = 5 - 4 = 1
  • Stars: 2 * i - 1 = 2 * 4 - 1 = 7

Prints: " *******"

Iteration 5 (i = 5)

An informative banner about Pradhan Mantri Mudra Yojana (PMMY) featuring Prime Minister Narendra Modi, the PMMY logo, and the tagline "A Game Changer for Entrepreneurs.
PMMY’s Explosive Impact: Igniting Small Business Success in 2025
  • Spaces: n - i = 5 - 5 = 0
  • Stars: 2 * i - 1 = 2 * 5 - 1 = 9

Prints: "*********"

Python Star Pattern of Lower Reverse Pyramid

Now, the pattern starts to decrease in size.

Iteration 1 (i = 4)

  • Spaces: n - i = 5 - 4 = 1
  • Stars: 2 * i - 1 = 2 * 4 - 1 = 7

Prints: " *******"

Iteration 2 (i = 3)

  • Spaces: n - i = 5 - 3 = 2
  • Stars: 2 * i - 1 = 2 * 3 - 1 = 5

Prints: " *****"

Iteration 3 (i = 2)

  • Spaces: n - i = 5 - 2 = 3
  • Stars: 2 * i - 1 = 2 * 2 - 1 = 3

Prints: " ***"

Iteration 4 (i = 1)

  • Spaces: n - i = 5 - 1 = 4
  • Stars: 2 * i - 1 = 2 * 1 - 1 = 1

Prints: " *"

FAQs

1. How do I change the size of the triangle?

Modify n in the code. Example: n = 7 prints a triangle of height 7.

2. How do I print a hollow inverted right-aligned triangle star pattern in python?

Modify the inner loop to print spaces inside the triangle.

3. Can I use while loops instead of for loops?

Yes, while loops can be used, but for loops provide more readability.

4. What is the difference between left-aligned and right-aligned triangles?

Right-aligned triangles use spaces before the stars, while left-aligned ones start directly from the left.

5. How do I take user input for n?

Use n = int(input(“Enter number of rows: “)).

Conclusion

In this post, we explained several Python Star Pattern programs that can be made using only two loops. By knowing how to modify rows and columns, you will have no problem forming different Python Star Pattern patterns, such as right-angled triangles, inverted triangles, pyramids, and diamonds. Utilizing just simple for loops and adding smart conditions you are able to output these drawings quickly.

If you are a starter who is about to learn about loops or simply someone who wants to be better at designing patterns, then getting to know these Python Star Patterns will make you an even stronger logical thinker and a better coder. You may practice by trying different values of n that can be implemented through pattern alterations to your coding universe. Please, never give up on these Python Star Patterns because they will make you handle the matching problems at all levels of input and loop in Python even in your sleep.

1 thought on “Create Stunning Python Star Patterns Using Only 2 For Loops!”

Leave a Comment