Python – Star Pattern Programs

In this tutorial, we will look into different Python programs where we print different start patterns to the console.

Example : Print Rectangle Start Pattern

This is a basic example of printing star patterns.

Rest of the programs are some kind of a modification to the below program.

In the following program, we print a rectangle of length 6 and width 4.

Python Program

def drawPattern(a, b):
	for x in range(0, a):
		for y in range(0, b):
			print(' * ', end='')
		print('')
		
drawPattern(4, 8)
Run this program ONLINE

Output

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

Example : Print Right Angled Triangle Star Pattern

In this example, we shall write a function to print a right angle triangle star pattern. This function takes an integer as argument. This integer represents the height of the triangle.

Python Program

def drawPattern(n):
	for x in range(0, n):
		for y in range(0, n):
			if x>=y:
				print(' * ', end='')
		print('')
		
drawPattern(6)
Run this program ONLINE

Output

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

Example – Pyramid Star Pattern

In this example, we shall write a function to print a pyramid star pattern. This function takes an integer as argument. This integer represents the height of the pyramid.

Python Program

def drawPattern(a):
	for x in range(0, a):
		for y in range(0, int(a-x)):
			print('   ', end='')
		for y in range(0, 2*x+1):
			print(' * ', end='')
		print('')
		
drawPattern(4)
Run this program ONLINE

Output

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

Example : Inverted Pyramid Start Pattern

In this example, print an inverted pyramid star pattern. This should be like reflection of the pyramid in the above program along horizontal axis. This function takes an integer as argument. This integer represents the height of the pyramid.

Python Program

def drawPattern(a):
	for x in range(0, a):
		for y in range(0, x):
			print('   ', end='')
		for y in range(0, 2*(a-x)-1):
			print(' * ', end='')
		print('')
		
drawPattern(6)
Run this program ONLINE

Output

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

Summary

In this tutorial of Python Examples, we have gone through different Python Star Pattern Programs that print different start patterns.