For working professionals
For fresh graduates
More
Pattern programs in Python are a fun and creative way to sharpen your coding skills. From star-filled designs to complex number pyramids, these programs allow you to explore the true power of loops and logical thinking.
But here’s the problem — many aspiring programmers find pattern programming intimidating. Without a clear understanding of rows, columns, and formatting, creating even basic patterns can feel like trying to solve a puzzle without any clues.
The good news is that writing pattern programs doesn’t have to be difficult. By mastering a few fundamental techniques, you can simplify complex patterns into simple, step-by-step tasks using Python’s efficient tools.
In this article, you’ll discover how to create number, star, and alphabet patterns with ease. By following the provided examples and tips, you’ll not only boost your confidence but also gain practical skills to tackle advanced programming challenges!
“Enhance your Python skills further with our Data Science and Machine Learning courses from top universities — take the next step in your learning journey!”
Let’s get started!
To print patterns in Python, you can follow a systematic approach that involves understanding how to structure your loops and format the output. Here is a clear, step-by-step explanation of how to print patterns:
Before you begin coding, decide how many rows and columns your pattern will have. This decision can be based on either user input or predefined values.
For example, if you want to create a pyramid pattern, you should determine the number of rows, as this will define the shape of the pyramid.
To allow for user input, you can use the `input()` function, which enables users to specify how many rows they want.
rows = int(input("Enter the number of rows: "))
for i in range(rows):
# Inner loop will go here
for j in range(i + 1): # Example for a right triangle pattern
# Print symbol or number here
Using the print() Function: Within the inner loop, use the `print()` function to display symbols (like `*`) or numbers (like `1`, `2`, etc.). You can control whether to print on the same line or move to a new line using the `end` parameter.
print("*", end=' ') # This will print stars on the same line
Example Patterns: For a simple star pattern
for i in range(rows):
for j in range(i + 1):
print("*", end=' ')
print() # Move to the next line after each row
Formatting Output for Better Readability
for i in range(rows):
print(' ' * (rows - i - 1), end='') # Print leading spaces
for j in range(i + 1):
print("*", end=' ')
print() # Move to next line after each row
Till now, you have a clear idea of how to print patterns in Python. Let’s take an example to compile all the steps.
Example: Pattern Program in Python to Print Pyramid
# Step 1: Decide number of rows based on user input
rows = int(input("Enter the number of rows: "))
# Step 2: Iterate through each row
for i in range(rows):
# Step 3: Print leading spaces for centering the pyramid
print(' ' * (rows - i - 1), end='')
# Step 4: Iterate through columns and print stars
for j in range(i + 1):
print("*", end=' ')
# Step 5: Add a new line after each row is printed
print()
Output
*
* *
* * *
* * * *
* * * * *
“Start your coding journey with our complimentary Python courses designed just for you — dive into Python programming fundamentals, explore key Python libraries, and engage with practical case studies!”
Now, it’s time to check 20+ patterns that we can print through Python programming.
# Function to print a square number pattern
def square_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for columns
for j in range(1, n + 1):
print(j, end=" ") # Print numbers in a row
print() # Move to the next line after each row
# Example: Print a 5x5 square pattern
square_pattern(5)
Output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Left Triangle Pattern
# Function to print a left triangle number pattern
def left_triangle_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for printing numbers
for j in range(1, i + 1):
print(j, end=" ") # Print numbers in each row
print() # Move to the next line after each row
# Example: Print a left triangle pattern of height 5
left_triangle_pattern(5)
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Right Triangle Pattern
# Function to print a right triangle number pattern
def right_triangle_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for spaces (to align the triangle to the right)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing numbers
for j in range(1, i + 1):
print(j, end=" ") # Print numbers in each row
print() # Move to the next line after each row
# Example: Print a right triangle pattern of height 5
right_triangle_pattern(5)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pyramid Number Pattern
# Function to print a pyramid number pattern
def pyramid_number_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for spaces (to center-align the pyramid)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing numbers
for j in range(1, i + 1):
print(j, end=" ")
# Inner loop for reverse printing numbers
for j in range(i - 1, 0, -1):
print(j, end=" ")
print() # Move to the next line after each row
# Function to print a pyramid number pattern
def pyramid_number_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for spaces (to center-align the pyramid)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing numbers
for j in range(1, i + 1):
print(j, end=" ")
# Inner loop for reverse printing numbers
for j in range(i - 1, 0, -1):
print(j, end=" ")
print() # Move to the next line after each row
# Example: Print a pyramid number pattern of height 5
pyramid_number_pattern(5)
Output
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Inverted Number Pattern
# Function to print an inverted number pattern
def inverted_number_pattern(n):
# Outer loop for rows
for i in range(n, 0, -1):
# Inner loop for printing numbers
for j in range(1, i + 1):
print(j, end=" ") # Print numbers in each row
print() # Move to the next line after each row
# Example: Print an inverted number pattern of height 5
inverted_number_pattern(5)
Output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Star Pattern Program in Python
# Function to print a full pyramid star pattern
def full_pyramid_star_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for spaces (to center-align the pyramid)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing stars
for j in range(2 * i - 1):
print("*", end=" ")
print() # Move to the next line after each row
# Example: Print a full pyramid star pattern of height 5
full_pyramid_star_pattern(5)
Output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Inverted Full Pyramid Pattern
# Function to print an inverted full pyramid star pattern
def inverted_full_pyramid_star_pattern(n):
# Outer loop for rows (from top to bottom)
for i in range(n, 0, -1):
# Inner loop for spaces (to center-align the pyramid)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing stars
for j in range(2 * i - 1):
print("*", end=" ")
print() # Move to the next line after each row
# Example: Print an inverted full pyramid star pattern of height 5
inverted_full_pyramid_star_pattern(5)
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Diamond Star Pattern
# Function to print a diamond star pattern
def diamond_star_pattern(n):
# Upper half of the diamond (including the middle row)
for i in range(1, n + 1):
# Inner loop for spaces
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing stars
for j in range(2 * i - 1):
print("*", end=" ")
print() # Move to the next line after each row
# Lower half of the diamond (excluding the middle row)
for i in range(n - 1, 0, -1):
# Inner loop for spaces
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing stars
for j in range(2 * i - 1):
print("*", end=" ")
print() # Move to the next line after each row
# Example: Print a diamond star pattern of height 5
diamond_star_pattern(5)
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Right Triangle Star Pattern
# Function to print a right triangle star pattern
def right_triangle_star_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for printing stars
for j in range(1, i + 1):
print("*", end=" ")
print() # Move to the next line after each row
# Example: Print a right triangle star pattern of height 5
right_triangle_star_pattern(5)
Output
*
* *
* * *
* * * *
* * * * *
Hollow Square Star Pattern
# Function to print a hollow square star pattern
def hollow_square_star_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for columns
for j in range(1, n + 1):
# Print stars for the border cells (first or last row/column)
if i == 1 or i == n or j == 1 or j == n:
print("*", end=" ")
else:
print(" ", end=" ") # Print space for inner cells
print() # Move to the next line after each row
# Example: Print a hollow square star pattern of size 5
hollow_square_star_pattern(5)
* * * * *
* *
* *
* *
* * * * *
Character Pattern Program in Python
# Function to print an alphabetical half pyramid
def alphabetical_half_pyramid(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for printing letters
for j in range(i):
print(chr(65 + j), end=" ") # chr(65) = 'A', chr(66) = 'B', and so on
print() # Move to the next line after each row
# Example: Print an alphabetical half pyramid of height 5
alphabetical_half_pyramid(5)
Output
A
A B
A B C
A B C D
A B C D E
Alphabetical Full Pyramid
# Function to print an alphabetical full pyramid
def alphabetical_full_pyramid(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for spaces (to center-align the pyramid)
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing letters
for j in range(i):
print(chr(65 + j), end=" ")
# Inner loop for reverse printing letters
for j in range(i - 2, -1, -1):
print(chr(65 + j), end=" ")
print() # Move to the next line after each row
# Example: Print an alphabetical full pyramid of height 5
alphabetical_full_pyramid(5)
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
Reverse Alphabetical Half Pyramid
# Function to print a reverse alphabetical half pyramid
def reverse_alphabetical_half_pyramid(n):
# Outer loop for rows
for i in range(n, 0, -1):
# Inner loop for printing letters
for j in range(i):
print(chr(65 + j), end=" ") # chr(65) = 'A', chr(66) = 'B', and so on
print() # Move to the next line after each row
# Example: Print a reverse alphabetical half pyramid of height 5
reverse_alphabetical_half_pyramid(5)
Output
A B C D E
A B C D
A B C
A B
A
Alphabetical Diamond Pattern
# Function to print an alphabetical diamond pattern
def alphabetical_diamond_pattern(n):
# Upper half of the diamond (including the middle row)
for i in range(1, n + 1):
# Inner loop for spaces
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing letters
for j in range(i):
print(chr(65 + j), end=" ")
# Inner loop for reverse printing letters
for j in range(i - 2, -1, -1):
print(chr(65 + j), end=" ")
print() # Move to the next line after each row
# Lower half of the diamond (excluding the middle row)
for i in range(n - 1, 0, -1):
# Inner loop for spaces
for space in range(n - i):
print(" ", end=" ")
# Inner loop for printing letters
for j in range(i):
print(chr(65 + j), end=" ")
# Inner loop for reverse printing letters
for j in range(i - 2, -1, -1):
print(chr(65 + j), end=" ")
print() # Move to the next line after each row
# Example: Print an alphabetical diamond pattern of height 5
alphabetical_diamond_pattern(5)
Output
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A
Hollow Alphabet Square Pattern
# Function to print a hollow alphabet square pattern
def hollow_alphabet_square_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for columns
for j in range(1, n + 1):
# Print letters for the border cells (first or last row/column)
if i == 1 or i == n or j == 1 or j == n:
print(chr(64 + j), end=" ") # chr(64 + j) gives 'A', 'B', etc.
else:
print(" ", end=" ") # Print space for inner cells
print() # Move to the next line after each row
# Example: Print a hollow alphabet square pattern of size 5
hollow_alphabet_square_pattern(5)
Output
A B C D E
A E
A E
A E
A B C D E
Advanced Pattern Program in Python
# Function to print Pascal's Triangle
def pascals_triangle(n):
# Loop to iterate over rows
for i in range(n):
# Print spaces to center-align the triangle
print(" " * (n - i), end="")
# Initialize the first value of each row
num = 1
# Loop to calculate and print each value in the row
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1) # Calculate the next value
print() # Move to the next row
# Example: Print Pascal's Triangle of height 5
pascals_triangle(5)
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Spiral Number Pattern
# Function to print a spiral number pattern
def spiral_number_pattern(n):
# Initialize a 2D array with zeroes
spiral = [[0] * n for _ in range(n)]
# Initialize variables for directions and boundaries
top, bottom, left, right = 0, n - 1, 0, n - 1
num = 1
# Fill the array in a spiral order
while top <= bottom and left <= right:
# Fill the top row
for i in range(left, right + 1):
spiral[top][i] = num
num += 1
top += 1
# Fill the right column
for i in range(top, bottom + 1):
spiral[i][right] = num
num += 1
right -= 1
# Fill the bottom row
for i in range(right, left - 1, -1):
spiral[bottom][i] = num
num += 1
bottom -= 1
# Fill the left column
for i in range(bottom, top - 1, -1):
spiral[i][left] = num
num += 1
left += 1
# Print the spiral matrix
for row in spiral:
print(" ".join(f"{x:2}" for x in row))
# Example: Print a spiral number pattern of size 5
spiral_number_pattern(5)
Output
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Hourglass Number Pattern
# Function to print an hourglass number pattern
def hourglass_number_pattern(n):
# Upper half of the hourglass
for i in range(n, 0, -1):
# Print leading spaces
print(" " * (n - i), end="")
# Print numbers in decreasing order
for j in range(1, 2 * i):
print(j, end="")
print() # Move to the next line
# Lower half of the hourglass
for i in range(2, n + 1):
# Print leading spaces
print(" " * (n - i), end="")
# Print numbers in decreasing order
for j in range(1, 2 * i):
print(j, end="")
print() # Move to the next line
# Example: Print an hourglass number pattern of height 5
hourglass_number_pattern(5)
Output
123456789
1234567
12345
123
1
123
12345
1234567
123456789
Multiplication Table Pattern
# Function to print a multiplication table pattern
def multiplication_table_pattern(n):
# Outer loop for rows
for i in range(1, n + 1):
# Inner loop for columns
for j in range(1, n + 1):
print(f"{i * j:3}", end=" ") # Print the product with formatting
print() # Move to the next line
# Example: Print a multiplication table of size 5
multiplication_table_pattern(5)
Output
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Hollow Diamond Star Pattern
# Function to print a hollow diamond star pattern
def hollow_diamond_star_pattern(n):
# Upper half of the diamond (including the middle row)
for i in range(1, n + 1):
# Print leading spaces
print(" " * (n - i), end="")
# Print stars with spaces in between
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print() # Move to the next line
# Lower half of the diamond
for i in range(n - 1, 0, -1):
# Print leading spaces
print(" " * (n - i), end="")
# Print stars with spaces in between
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print() # Move to the next line
# Example: Print a hollow diamond star pattern of height 5
hollow_diamond_star_pattern(5)
Output
*
* *
* *
* *
* *
* *
* *
* *
*
With upGrad, you can access global standard education facilities right here in India. upGrad also offers free Python courses that come with certificates, making them an excellent opportunity if you're interested in data science and machine learning.
By enrolling in upGrad's Python courses, you can benefit from the knowledge and expertise of some of the best educators from around the world. These instructors understand the diverse challenges that Python programmers face and can provide guidance to help you navigate them effectively.
So, reach out to an upGrad counselor today to learn more about how you can benefit from a Python course.
Here are some of the best data science and machine learning courses offered by upGrad, designed to meet your learning needs:
Similar Reads: Top Trending Blogs of Python
Pattern programs involve using loops to display various shapes or patterns, such as stars, numbers, or alphabets, in a structured format.
Patterns are typically created using nested loops. The outer loop manages the number of rows, while the inner loop controls the content of each row.
Common patterns include pyramids, inverted pyramids, right-angled triangles, diamonds, and hourglass shapes, using characters like stars (*) or numbers.
Yes, while loops can be used to create patterns, though for loops are more common due to their straightforward iteration over a sequence.
While Python primarily deals with 2D text-based patterns, advanced manipulation of spacing, symbols, and layering can simulate 3D effects, like a cube or a sphere, using characters.
Using string concatenation techniques and minimizing repetitive operations inside loops can significantly improve the efficiency of pattern generation for large-scale designs.
Yes, NumPy arrays can be used to generate grid-based patterns more efficiently, especially for larger patterns, leveraging its array manipulation capabilities.
Ready to challenge yourself? Take our Free Python Quiz!
Pavan Vadapalli
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.