Python – Star Pattern Programs

Star Pattern Programs

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

1. 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 Code Copy

Output

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

2. 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 Code Copy

Output

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

3. 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 Code Copy

Output

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

4. 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 Code Copy

Output

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

Summary

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

Related Tutorials

Code copied to clipboard successfully 👍