Pattern Program in Python: 40+ Star, Number & Alphabet Patterns
By Rohit Sharma
Updated on Jul 07, 2026 | 38 min read | 52.18K+ views
Share:
All courses
Certifications
More
By Rohit Sharma
Updated on Jul 07, 2026 | 38 min read | 52.18K+ views
Share:
Table of Contents
Quick Overview:
In this blog you will learn how to write pattern programs in Python using for loops, while loops, nested loops, and recursion, with star, number, alphabet, pyramid, diamond, and hollow pattern examples along with code and output.
Strengthen your Python skills with upGrad's Data Science Course. Learn Python, SQL, machine learning, and data analytics through hands-on projects and expert-led training.
Popular Data Science Programs
A pattern program in Python is a coding exercise that uses loops to print symbols, numbers, or characters in a specific geometric arrangement. These programs help you understand iteration, nested loops, conditional statements, and logical thinking. From simple star pattern programs in Python to advanced diamond pattern program in Python, pattern questions are widely used in coding interviews and programming practice.
For learners preparing to transition into high-growth tech careers, like becoming an AI Simulation Engineer or Data Analyst, spending just 30 to 45 minutes a day tracing these nested loops on paper can drastically cut down the time it takes to fully master Python.
Writing a pattern program starts with deciding the number of rows and the output format. Most patterns use an outer loop to control rows and one or more inner loops to print stars, numbers, alphabets, or spaces. A pattern program in Python using for loop is the most common approach because it is simple to read and implement.
Example:
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print("*", end=" ")
print()
Output:
*
* *
* * *
* * * *
* * * * *
Pattern programs work by repeating a sequence of characters through nested loops. The outer loop controls the number of rows, while the inner loop determines how many symbols, numbers, or spaces appear in each row. By changing the loop conditions, you can create increasing, decreasing, pyramid, or symmetrical patterns.
Working:
Example:
rows = 5
for i in range(rows, 0, -1):
for j in range(i):
print("*", end=" ")
print()
Output:
* * * * *
* * * *
* * *
* *
*
The pattern program in Python using for loop is the preferred approach because for loops provide precise control over the number of iterations. They are commonly used to create triangles, pyramids, diamonds, and other geometric patterns.
Example:
rows = 5
for i in range(1, rows + 1):
print("* " * i)
Output:
*
* *
* * *
* * * *
* * * * *
A while loop is useful when the number of iterations depends on a condition instead of a predefined range. It produces the same output as a for loop but requires manual control of loop variables.
Example:
rows = 5
i = 1
while i <= rows:
j = 1
while j <= i:
print("*", end=" ")
j += 1
print()
i += 1
Output:
*
* *
* * *
* * * *
* * * * *
Nested loops are the foundation of advanced pattern printing. They combine multiple loops to print stars, numbers, and spaces in a structured format. Most number pattern programs in Python and pyramid or diamond patterns rely on nested loops.
Example: Diamond Pattern Program in Python
rows = 5
for i in range(rows):
print(" " * (rows - i - 1) + "*" * (2 * i + 1))
for i in range(rows - 2, -1, -1):
print(" " * (rows - i - 1) + "*" * (2 * i + 1))
Output:
*
***
*****
*******
*********
*******
*****
***
*
This approach can be adapted to build star pattern programs in Python, number pattern programs in Python, hollow patterns, alphabetical patterns, and the diamond pattern program in Python by changing the values printed inside the inner loops.
Star pattern programs in Python are the classic starting point. They help you understand the structure of rows and columns using a simple, single character.
The outer loop controls the rows, and the inner loop prints stars equal to the current row number.
Python
def pattern1(n):
for i in range(1, n + 1):
print("*" * i)
pattern1(5)
Output:
*
**
***
****
*****
The outer loop counts down from the total number of rows.
Python
def pattern2(n):
for i in range(n, 0, -1):
print("*" * i)
pattern2(5)
Output:
*****
****
***
**
*
This pattern requires an extra loop to print leading spaces.
Python
def pattern3(n):
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)
pattern3(5)
Output:
*
**
***
****
*****
Also Read: Top 50 Python Project Ideas with Source Code in 2025
This combines a countdown loop for stars with an increasing loop for spaces.
Python
def pattern4(n):
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * i)
pattern4(5)
Output:
*****
****
***
**
*
This pattern combines spaces and an increasing number of stars centered in each row.
Python
def pattern5(n):
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern5(5)
Output:
*
***
*****
*******
*********
Also Read: Top 36+ Python Projects for Beginners in 2026
The reverse of the pyramid pattern, with loops counting downwards.
Python
def pattern6(n):
for i in range(n - 1, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern6(5)
Output:
*********
*******
*****
***
*
A diamond is created by printing a pyramid followed by an inverted pyramid.
Python
def pattern7(n):
# Upper part
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
# Lower part
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern7(5)
Output:
*
***
*****
*******
*********
*******
*****
***
*
An hourglass is an inverted pyramid followed by a regular pyramid.
Python
def pattern8(n):
# Upper part
for i in range(n - 1, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
# Lower part
for i in range(1, n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern8(5)
Output:
*********
*******
*****
***
*
***
*****
*******
*********
This uses conditional logic to print stars only for the border.
Python
def pattern9(n):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
pattern9(5)
Output:
* * * * *
* *
* *
* *
* * * * *
A right triangle and an inverted right triangle joined together.
Python
def pattern11(n):
for i in range(1, n + 1):
print("*" * i)
for i in range(n - 1, 0, -1):
print("*" * i)
pattern11(5)
Output:
*
**
***
****
*****
****
***
**
*
Also Read: Enhance Your Python Skills: 10 Python Projects You Need to Try!
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
Number pattern programs in python replace the stars with numbers, often based on the row or column index, to create more complex logical challenges.
Prints the row number repeatedly in each row.
Python
def pattern16(n):
for i in range(1, n + 1):
for j in range(i):
print(i, end=" ")
print()
pattern16(5)
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Prints numbers sequentially from 1 up to the current column number.
Python
def pattern17(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern17(5)
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Also Read: 20+ Data Science Projects in Python for Every Skill Level
This pattern prints consecutive numbers in a right triangle shape.
Python
def pattern18(n):
num = 1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
pattern18(5)
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Prints a pattern of alternating 0s and 1s based on column position.
Python
def pattern19(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j % 2, end=" ")
print()
pattern19(5)
Output:
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
A famous mathematical pattern where each number is the sum of the two numbers directly above it.
Python
def pattern20(n):
for i in range(n):
print(" " * (n - i - 1), end="")
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()
pattern20(5)
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Advance your career with the Master of Science in Data Science from Liverpool John Moores University. Build expertise in Python, machine learning, AI, and analytics through industry-focused learning.
Prints descending numbers in an inverted triangle.
Python
def pattern21(n):
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern21(5)
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Also Read: Top Python Automation Projects & Topics For Beginners
Creates a pyramid where each row is a palindrome.
Python
def pattern22(n):
for i in range(1, n + 1):
print(" " * (n - i), end="")
# Ascending part
for j in range(1, i + 1):
print(j, end="")
# Descending part
for j in range(i - 1, 0, -1):
print(j, end="")
print()
pattern22(5)
Output:
1
121
12321
1234321
123454321
8. Square of Numbers
Prints a square where each row contains the row number.
Python
def pattern23(n):
for i in range(1, n + 1):
for j in range(n):
print(i, end=" ")
print()
pattern23(5)
Output:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
A diamond shape made of numbers, increasing to a center row.
Python
def pattern24(n):
# Upper part
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
print()
# Lower part
for i in range(n - 1, 0, -1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern24(5)
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
A pyramid pattern made of alternating zeros and ones.
Python
def pattern25(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
if (i + j) % 2 == 0:
print(1, end=" ")
else:
print(0, end=" ")
print()
pattern25(5)
Output:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
The reverse of the sequential number triangle, counting down rows.
Python
def pattern26(n):
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j, end=" ")
print()
pattern26(5)
Output:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
The numbers increase down the columns instead of across the rows.
Python
def pattern27(n):
for i in range(1, n + 1):
val = i
for j in range(1, i + 1):
print(val, end=" ")
val += n - j
print()
pattern27(5)
Output:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
A square of numbers that increments down the columns.
Python
def pattern28(n):
for i in range(1, n + 1):
for j in range(n):
print(i + j * n, end=" ")
print()
pattern28(5)
Output:
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
A rectangle of numbers that alternates its order in each row.
Python
def pattern29(n):
for i in range(1, n + 1):
if i % 2 != 0:
for j in range(1, n + 1):
print(j, end=" ")
else:
for j in range(n, 0, -1):
print(j, end=" ")
print()
pattern29(5)
Output:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
A pyramid where each row is a number that reads the same forwards and backwards.
Python
def pattern30(n):
for i in range(1, n + 1):
print(" " * (n - i), end="")
print(str(11 ** (i - 1)).replace("0", " "))
pattern30(5)
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Also Read: 30 Must-Try Python Django Project Ideas & Topics For Beginners in 2025
These patterns use alphabets, which is a great way to practice working with ASCII values using Python's ord() and chr() functions.
Prints the character corresponding to the row number repeatedly.
Python
def pattern31(n):
for i in range(n):
char = chr(ord('A') + i)
print(char * (i + 1))
pattern31(5)
Output:
A
BB
CCC
DDDD
EEEEE
Prints alphabets in sequence up to the current row.
Python
def pattern32(n):
for i in range(n):
row_string = ""
for j in range(i + 1):
row_string += chr(ord('A') + j)
print(row_string)
pattern32(5)
Output:
A
AB
ABC
ABCD
ABCDE
Prints alphabets continuously across all rows and columns.
Python
def pattern33(n):
char_code = ord('A')
for i in range(n):
for j in range(i + 1):
print(chr(char_code), end=" ")
char_code += 1
print()
pattern33(5)
Output:
A
B C
D E F
G H I J
K L M N O
Forms the shape of the letter 'K' using any character.
Python
def pattern34(n, char='K'):
for i in range(n, 0, -1):
print(char * i)
for i in range(2, n + 1):
print(char * i)
pattern34(5, 'K')
Output:
KKKKK
KKKK
KKK
KK
K
KK
KKK
KKKK
KKKKK
A pyramid shape made of alphabets.
Python
def pattern35(n):
for i in range(n):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print(char * (2 * i + 1))
pattern35(5)
Output:
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
Prints a square where each row contains the same character.
Python
def pattern36(n):
for i in range(n):
char = chr(ord('A') + i)
print((char + " ") * n)
pattern36(5)
Output:
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E
A diamond shape where each row contains the corresponding alphabet character.
Python
def pattern37(n):
# Upper part
for i in range(n):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print((char + " ") * (i + 1))
# Lower part
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print((char + " ") * (i + 1))
pattern37(5)
Output:
A
B B
C C C
D D D D
E E E E E
D D D D
C C C
B B
A
Forms a hollow square using alphabet characters.
Python
def pattern38(n):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print(chr(ord('A') + j), end=" ")
else:
print(" ", end=" ")
print()
pattern38(5)
Output:
A B C D E
A E
A E
A E
A B C D E
A pyramid where each row is a palindromic sequence of characters.
Python
def pattern39(n):
for i in range(n):
print(" " * (n - i - 1), end="")
# Ascending
for j in range(i + 1):
print(chr(ord('A') + j), end="")
# Descending
for j in range(i - 1, -1, -1):
print(chr(ord('A') + j), end="")
print()
pattern39(5)
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
An inverted triangle with sequential alphabets.
Python
def pattern40(n):
for i in range(n, 0, -1):
row_string = ""
for j in range(i):
row_string += chr(ord('A') + j)
print(row_string)
pattern40(5)
Output:
ABCDE
ABCD
ABC
AB
A
Solving a pattern program in python can also be done with recursion. This is a more advanced approach where a function calls itself to solve a problem.
Python
def pattern41(n, current_row=1):
if current_row > n:
return
print("*" * current_row)
pattern41(n, current_row + 1)
pattern41(5)
Output:
*
**
***
****
*****
Python
def pattern42(n):
if n == 0:
return
print("*" * n)
pattern42(n - 1)
pattern42(5)
Output:
*****
****
***
**
*
Python
def pattern43(n, current_row=0):
if current_row == n:
return
print(" " * (n - current_row - 1) + "*" * (2 * current_row + 1))
pattern43(n, current_row + 1)
pattern43(5)
Output:
*
***
*****
*******
*********
Also Read: Top 50 Python AI & Machine Learning Open-source Projects
Python
def pattern44(n, current_row=1):
if current_row > n:
return
print(" " * (n - current_row) + "*" * current_row)
pattern44(n, current_row + 1)
pattern44(5)
Output:
*
**
***
****
*****
Python
def pattern45_helper(n, i, j):
if j == n:
print()
return
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
pattern45_helper(n, i, j + 1)
def pattern45(n, i=0):
if i == n:
return
pattern45_helper(n, i, 0)
pattern45(n, i + 1)
pattern45(5)
Output:
* * * * *
* *
* *
* * * * *
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
Pattern programs are simple to understand but easy to get wrong when working with loops and spacing. Avoiding these common mistakes helps you write cleaner code and produce the expected output.
| Mistake | How to Avoid It |
|---|---|
| Using incorrect loop ranges | Verify the start, stop, and step values in your for or while loops before running the program. |
| Forgetting nested loops | Use an outer loop for rows and an inner loop for columns when printing patterns. |
| Printing on a new line every time | Use end=" " to print values on the same line and print() only after each row is complete. |
| Incorrect spacing | Count spaces carefully, especially for pyramid and diamond patterns, to keep the output aligned. |
| Not resetting loop variables | Reinitialize counters inside while loops before printing each row. |
| Confusing rows and columns | Let the outer loop control rows and the inner loop control the number of characters or numbers printed. |
| Hardcoding the number of rows | Store the row count in a variable so the pattern size can be changed easily. |
| Ignoring edge cases | Test your program with values like 0, 1, and larger row counts to verify the output. |
| Missing indentation | Maintain proper indentation because Python uses it to define loop blocks. |
| Not tracing the logic | Dry-run the program row by row to understand how loops generate the final pattern. |
After we get to the code, it is helpful to understand why these exercises are so valuable. Solving a pattern program in python is more than just a simple coding challenge.
Also Read: 3 Best Raspberry Pi Python Projects [For Freshers & Experienced]
Practicing pattern programs in Python helps you build a strong foundation in loops, conditions, and logical thinking. Starting with simple patterns and gradually moving to more complex ones improves your coding skills and confidence.
As you solve more star pattern programs in Python and other pattern-based exercises, you'll develop a better understanding of nested loops and problem-solving techniques that are useful in coding interviews and real-world programming.
Similar Reads:
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
The most common mistake is mixing up the logic of the outer and inner loops. The outer loop usually controls the number of rows, and the inner loop handles what happens within each row, like printing stars, numbers, or spaces. Beginners often swap their roles or forget to reset the inner loop logic for each row. A simple way to avoid this is to always ask: “What repeats per row?” (inner loop) and “How many rows are needed?” (outer loop). Once you separate those, the logic becomes clear.
Yes. Any pattern program in Python using for loop can also be written with while loops. The difference is that with while, you need to initialize a counter before the loop and increment it manually inside the loop. For example:
i = 0
while i < 5:
print("*" * (i+1))
i += 1
This produces a right triangle. While loops give you more control, but for loops are simpler for patterns since they work naturally with the range() function.
Normally, print() moves the cursor to the next line after printing. For patterns, we often want to keep printing on the same line. That’s where end="" comes in. It changes what happens after printing. For example:
print("*", end=" ")
keeps printing stars on the same line with a space in between. Without this, every star would be on a new line, which breaks the pattern shape.
When you need alphabet patterns like A-B-C triangles or sequences, you can add numbers to ASCII values and then convert them back to letters. Example:
for i in range(5):
print(chr(65 + i))
This prints letters A to E. These two functions are the backbone of alphabet-based patterns.
No. Loops are more efficient for patterns. Recursion calls the same function repeatedly, which takes more memory because each call is stored in the stack. But recursion is still useful for learning. For example, a recursive triangle pattern shows how a bigger problem can be broken into smaller ones. If efficiency is your goal, stick with loops, but if understanding recursion is the goal, try both.
You can use the input() function to let the user decide the number of rows or size. For example:
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
print("*" * i)
This makes your code flexible. Instead of hardcoding sizes, you can print different shapes based on user input. Always remember to convert input() into int() because it returns a string by default.
A nested loop is one loop inside another. In pattern programs:
For example:
for i in range(5):
for j in range(i+1):
print("*", end="")
print()
Yes. Instead of "*", you can use any character, symbol, or even emoji (if allowed). For example:
for i in range(5):
print("#" * (i+1))
This prints a triangle with #. You can also mix symbols, like alternating * and @ to form unique designs. It’s all about what character you put inside print().
Patterns like pyramids, diamonds, or left-aligned triangles need alignment. To create that alignment, you print spaces before printing stars or numbers. Example:
for i in range(5):
print(" " * (5-i-1) + "*" * (i+1))
Here, " " * (5-i-1) prints spaces before stars, which shifts them to the right, creating a pyramid shape. Without this spacing logic, your pyramid would just look like a right triangle.
You can combine smaller patterns to form bigger shapes. For example, a diamond pattern is a pyramid followed by an inverted pyramid. By calling the functions one after another, you get a complete diamond. Combination is about recognizing that big patterns are made from smaller ones stacked together.
range() generates a sequence of numbers that you can loop through. Examples:
This is powerful in patterns because you decide how many rows to loop, whether to count up or down, and how many steps to skip.
To print an inverted or reverse pattern, you adjust the range(). For example:
for i in range(5, 0, -1):
print("*" * i)
This prints 5 stars first, then 4, down to 1. By decreasing instead of increasing, you flip the direction of the pattern.
This difference is what allows us to print multiple symbols in a row before moving to the next line. Without end="", patterns would break line by line.
Aligned patterns are created by printing spaces before the main symbols. Pyramid, diamond, and centered triangle patterns use one loop to print leading spaces and another loop to print stars or numbers. Adjusting the number of spaces in each row keeps the output symmetrical and properly aligned.
A hollow pattern prints only the borders of the shape while leaving the inside empty. Example:
*****
* *
* *
*****
To create this, you use conditions: print stars at the border (first row, last row, first column, last column), and print spaces inside. This combines if/else with nested loops, which makes it slightly trickier but very rewarding to code.
Don’t try to code it all at once. Break the problem into smaller pieces. Example: For a diamond:
Once you have each part working, join them. Also, sketch the pattern on paper first to see the rows, spaces, and symbols. This makes it much easier to translate into code.
You can reduce repeated code by creating functions for common tasks such as printing rows or spaces. Reusable functions make pattern programs shorter, easier to maintain, and simpler to modify when creating different star, number, or alphabet patterns.
Start with simple right triangles before moving to pyramids, diamonds, and hollow patterns. Practice one new pattern each day, understand how nested loops work, and dry-run your code to track each iteration. Consistent practice helps improve both coding speed and logical thinking.
To solve pattern programs, you should understand variables, for and while loops, nested loops, conditional statements, the range() function, and the print() function with the end parameter. These concepts form the foundation for building simple and advanced patterns.
Pattern programs strengthen logical thinking by teaching you how to break complex problems into smaller steps. They improve your understanding of loops, conditions, and iteration, making it easier to solve algorithmic problems involving arrays, strings, recursion, and data structures.
892 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources