Python Print Star Patterns 14 Programs

By | May 17, 2021

Python Print Star Pattern Shapes – In this Python Programming tutorial, we will discuss printing different star patterns, shapes, pyramids, triangles, squares etc using nested for loops.

14 Python Print Star Patterns

The Program Logic

Normally we use nested for loops to print star shapes in Python.

For example, the following Python program will need two nested for loops. The outer for loop is for rows and the inner for loop is for columns or stars.

We use first for loop from 1 to N where N is the number of rows. Similarly, second for loop is used to print stars. We know that there is only one star in first row. There are two stars in line number two and so on. Hence we use second for loop from 1 to i. where i is the row number.

Print Python Star Pattern 1

# Python Program to print star pattern
'''
*
* *
* * *
* * * *
* * * * *
'''
# define a Function
def star_pattern1(n):
    # Use outer for loop for rows

    for i in range(1, n+1):

        # Use inner for loop for columns / stars

        for j in range(1, i + 1):
            # print stars in each row
            print("* ", end="")

        # End of line after each row
        print()

n = int(input("Enter number of rows:"))
star_pattern1(n)

Printing Star Shape 2 Using Nested For Loops

# Python Program to print star pattern 2
'''

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

'''
# define a function to print star shape
def star_pattern2(n):
    # Use outer for loop for rows

    for i in range(n, 0, -1 ):

        # Use inner for loop for columns / stars

        for j in range(1, i + 1):
            # print stars in each row
            print("* ", end="")

        # End of line after each row
        print()

n = int(input("Enter number of rows:"))
star_pattern2(n)

Output Star Pattern 2

Enter number of rows:5
* * * * * 
* * * * 
* * * 
* * 
* 

Printing Star Triangle Pattern Program 3 in Python

# Python Program to print star Triangle pattern 3
'''
     Enter number of rows:5
     *
    **
   ***
  ****
 *****

'''


# define a function to print star shape
def star_pattern3(n):
    # Use outer for loop for rows
    spaces = n
    for i in range(1, n + 1):

        # Use inner for loop 1  for spaces before stars

        for j in range(1, spaces + 1):
            # print spaces
            print(" ", end="")
        # Use inner for loop 2  for columns / stars

        for k in range(1, i + 1):
            # print stars in each row
            print("*", end="")

        # End of line after each row
        print()
        # decrease spaces
        spaces = spaces - 1


n = int(input("Enter number of rows:"))
star_pattern3(n)

Python Show Star Triangle 4

# Python Program to print star Triangle pattern 4
'''
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 

'''


# define a function to print star shape
def star_pattern4(n):
    # Use outer for loop for rows
    spaces = n
    for i in range(1, n + 1):

        # Use inner for loop 1  for spaces before stars

        for j in range(1, spaces + 1):
            # print spaces
            print(" ", end="")
        # Use inner for loop 2  for columns / stars

        for k in range(1, i + 1):
            # print stars in each row
            print("* ", end="")

        # End of line after each row
        print()
        # decrease spaces
        spaces = spaces - 1


n = int(input("Enter number of rows:"))
star_pattern4(n)

Python Program Solution-2

# Python program to print the
# following stars pattern
'''

Enter number of rows:8
        *
       * *
      * * *
     * * * *
    * * * * *
   * * * * * *
  * * * * * * *
 * * * * * * * *

'''
# define a user defined function to
# print star pattern
def star_patterns(n):
    space = 2 * n - 2
    for i in range(0, n):
        for j in range(0, space):
            print(" ",end="")
        space = space - 1
        for j in range(0, i + 1):
            print("*", end=" ")
        print()

# Input number of star rows
n = int(input("Enter number of Rows in Star Pattern:"))
# call the function to print the star pattern
star_patterns(n)

 

Hollow Square of Stars Pattern 5 in Python

# Python Program to Print Hollow Square 5 Star Pattern
'''
Please Enter any Side of a Square  : 5
Hollow Square Star Pattern
* * * * * 
*       * 
*       * 
*       * 
* * * * * 


'''
side = int(input("Please Enter any Side of a Square  : "))

print("Hollow Square Star Pattern")
for i in range(side):
    for j in range(side):
        if(i == 0 or i == side-1 or j == 0 or j == side-1):
            print('*', end = ' ')
        else:
            print(' ', end = ' ')
    print()

Filled Stars Square Pattern Python Program 6

# Python Program to Print Filled Square Star Pattern 6
'''
Please Enter any Side of a Square  : 5
Filled Square Star Pattern
* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 


'''
side = int(input("Please Enter any Side of a Square  : "))

print("Filled Square Star Pattern")
for i in range(side):
    for j in range(side):
        print('*', end = ' ')

    print()

Python Program To display star pattern number 7

# Program to print star patterns
'''
Enter number of rows=5
        * 
      * * 
    * * * 
  * * * * 
* * * * * 
  * * * * 
    * * * 
      * * 
        * 
'''
rows = int(input("Enter number of rows="))
i = 1
while i <= rows:
    j = i
    while j < rows:
        # printing spaces before stars
        print(' ', end=' ')
        j += 1
    k = 1
    # print stars
    while k <= i:
        print('*', end=' ')
        k += 1
    print()
    i += 1
# for downside output of star pattern
i = rows
while i >= 1:
    j = i
    while j <= rows:
        print(' ', end=' ')
        j += 1
    k = 1
    while k < i:
        print('*', end=' ')
        k += 1
    print('')
    i -= 1

Python star Patterns printing program 8

# Python Star Pattern Printing Program 8
'''
Enter number of rows:5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

'''

rows = int(input("Enter number of rows:"))
for i in range(1, rows+1):
    for j in range(1, i + 1):
        print("*", end=' ')
    print()

for i in range(rows, 1, -1):
    for j in range(1, i):
        print("*", end=' ')
    print()

Print Star Pattern 9

#Python Print star pattern 9
'''
Enter number of Rows:5
        * * * * * *
         * * * * *
          * * * *
           * * *
            * *
             *

'''

rows = int(input("Enter number of Rows:"))
space = 2 * rows - 2
for i in range(rows, -1, -1):
    for j in range(space, 0, -1):
        print(end=" ")
    space += 1
    for j in range(0, i + 1):
        print("*", end=" ")
    print("")

Sand Glass star pattern 10 in Python Programming

# Write a Python program
# to display Sand glass
# star pattern 10
'''
Enter number of Rows:5
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
'''

rows = int(input("Enter number of Rows:"))

for i in range(1,rows):
    for j in range(1,i):
        # print required number of spaces
        print('', end=' ')

    k = i
    for k in range(i,rows+1):
        print('*', end=' ')

    print()


i = rows - 1
while i >= 0:
    j = 0
    while j < i:
        print('', end=' ')
        j += 1
    k = i
    while k <= rows - 1:
        print('*', end=' ')
        k += 1
    print('')
    i -= 1

Print Trouser Style Pattern in Python 11

# Print Trouser star Pattern 11 Python program
'''
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *
'''
n = 16
print("*" * n, end="\n")
i = (n // 2) - 1
j = 2
while i != 0:
    while j <= (n - 2):
        print("*" * i, end="")

        print(" " * j, end="")
        print("*" * i)
        i = i - 1
        j = j + 2

Python Pyramid Program to Display Trouser Line Star Pattern

# Print Trouser star Lines Pattern 12 Python program
'''
****************
*******--*******
******----******
*****------*****
****--------****
***----------***
**------------**
*--------------*
'''
n = 16
print("*" * n, end="\n")
i = (n // 2) - 1
j = 2
while i != 0:
    while j <= (n - 2):
        print("*" * i, end="")

        print("-" * j, end="")
        print("*" * i)
        i = i - 1
        j = j + 2

Star diamond printing Python Program 13

#Python program Print star Diamond 13
'''
Enter number of rows:5
        *
       * *
      * * *
     * * * *
    * * * * *
   * * * * * * 
    * * * * *
     * * * *
      * * *
       * *
        *
'''
rows = int(input("Enter number of rows:"))
space = 2 * rows - 2
for i in range(0, rows):
    for j in range(0, space):
        print(end=" ")
    space = space - 1
    for j in range(0, i + 1):
        print("* ", end="")
    print("")

space = rows - 2

for i in range(rows, -1, -1):
    for j in range(space, 0, -1):
        print(end=" ")
    space = space + 1
    for j in range(0, i + 1):
        print("* ", end="")
    print("")

Python Hollow Star Diamond Print Program 14

# Python Hollow Star Diamond Pattern 14
'''
        * 
      *   *
    *       *
  *           *
*               *
  *           *
    *       *
      *   *
        * 
'''

rows = 5
i = 1
while i <= rows:
    j = rows
    while j > i:
        # display space
        print(' ', end=' ')
        j -= 1
    print('*', end=' ')
    k = 1
    while k < 2 * (i - 1):
        print(' ', end=' ')
        k += 1
    if i == 1:
        print()
    else:
        print('*')
    i += 1

i = rows - 1
while i >= 1:
    j = rows
    while j > i:
        print(' ', end=' ')
        j -= 1
    print('*', end=' ')
    k = 1
    while k <= 2 * (i - 1)-1:
        print(' ', end=' ')
        k += 1
    if i == 1:
        print()
    else:
        print('*')
    i -= 1

You may also like the following popular post on printing alphabets pyramids and patterns / shapes:

Python Print Alphabet Patterns 11 Programs

Program Display Star Triangle Shape

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *