Star Pattern Programs in Python
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)
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)
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)
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)
Output
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Summary
In this tutorial of Python Examples, we have gone through different Python Star Pattern Programs that print different start patterns.