Nearby lessons
54 of 159Python - Right-Aligned and Decreasing Triangle Patterns
- Recognize the structure of right-aligned and decreasing triangle patterns
- Apply the leading-space formula for right-aligned shapes
- Use chr() and ASCII values to print alphabets
- Distinguish repeated-value patterns from value-sequence patterns
- Compare the star, number, and alphabet triangle logic
Introduction
Part 3 introduces right-aligned and right-shifted pattern programs.
We will learn:
Pattern-21 → Decreasing Repeated Reverse Alphabets Pattern-22 → Decreasing Reverse Alphabet Sequence Pattern-23 → Right-Aligned Star Triangle without Spaces Pattern-24 → Right-Aligned Star Triangle with Spaces Pattern-25 → Right-Aligned Repeated Number Triangle Pattern-26 → Right-Aligned Increasing Number Triangle Pattern-27 → Right-Aligned Repeated Alphabet Triangle Pattern-28 → Right-Aligned Increasing Alphabet Triangle Pattern-29 → Right-Shifted Decreasing Star Triangle Pattern-30 → Right-Shifted Decreasing Repeated Number Triangle
The most important new concept is leading spaces.
Increasing right-aligned pattern: Spaces = n - i Values = i
As the row number increases:
Spaces ↓ Values ↑
Pattern-21 — Decreasing Repeated Reverse Alphabet Triangle
Pattern-21 prints alphabets from J to A.
The number of characters also decreases in every row.
J J J J J J J J J J I I I I I I I I I H H H H H H H H G G G G G G G F F F F F F E E E E E D D D D C C C B B A
For every row:
Character Count = n + 1 - i Character = chr(64 + n + 1 - i)
Pattern-21 — Complete Program
Pattern-21 — Program Explanation
Outer Loop
for i in range(1,n+1):
The outer loop controls the rows.
Inner Loop
for j in range(1,n+2-i):
The actual number of iterations is:
n + 1 - i
Therefore, the number of characters decreases row by row.
Character Formula
chr(64+n+1-i)
For n = 10:
i = 1 → chr(64+10+1-1)
→ chr(74)
→ J
i = 2 → chr(73)
→ I
i = 3 → chr(72)
→ H
...
i = 10 → chr(65)
→ A
Pattern-21 — Dry Run with n = 5
| i | Character | Times Printed | Output |
|---|---|---|---|
| 1 | E | 5 | E E E E E |
| 2 | D | 4 | D D D D |
| 3 | C | 3 | C C C |
| 4 | B | 2 | B B |
| 5 | A | 1 | A |
Pattern-22 — Decreasing Reverse Alphabet Sequence
Pattern-22 prints reverse alphabets in each row.
J I H G F E D C B A J I H G F E D C B J I H G F E D C J I H G F E D J I H G F E J I H G F J I H G J I H J I J
The first alphabet always starts from J when n = 10.
The number of alphabets decreases after every row.
Pattern-22 — Complete Program
Pattern-22 — Formula Explanation
The character formula is:
chr(65+n-j)
For n = 10:
j = 1 → chr(65+10-1)
→ chr(74)
→ J
j = 2 → chr(73)
→ I
j = 3 → chr(72)
→ H
Because the expression depends on j, the character changes inside the row.
Pattern-21 vs Pattern-22
| Pattern | Formula | Depends On | Result |
|---|---|---|---|
| 21 | chr(64+n+1-i) |
Row | Same alphabet repeated |
| 22 | chr(65+n-j) |
Column | Reverse alphabet sequence |
Pattern-21: E E E E E D D D D C C C B B A Pattern-22: E D C B A E D C B E D C E D E
Understanding Right-Aligned Patterns
Starting from Pattern-23, spaces are printed before the actual pattern.
The basic formula is:
" " * (n-i)
For n = 5:
| Row i | Leading Spaces | Pattern Size |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 0 | 5 |
So:
Leading Spaces = n - i Pattern Size = i
Pattern-23 — Right-Aligned Star Triangle without Spaces
Pattern-23 creates a right-aligned increasing star triangle.
*
**
***
****
*****
******
*******
********
*********
**********
The stars do not contain spaces between them.
Pattern-23 — Complete Program
Pattern-23 — Program Explanation
The statement:
" " * (n-i)
generates the leading spaces.
The statement:
"*" * i
generates the stars.
Therefore:
Spaces = n - i Stars = i
For n = 5:
i = 1 → 4 spaces + 1 star i = 2 → 3 spaces + 2 stars i = 3 → 2 spaces + 3 stars i = 4 → 1 space + 4 stars i = 5 → 0 spaces + 5 stars
Pattern-23 — Dry Run with n = 5
i = 1
" " * 4 + "*" * 1
*
i = 2
" " * 3 + "*" * 2
**
i = 3
" " * 2 + "*" * 3
***
i = 4
" " * 1 + "*" * 4
****
i = 5
" " * 0 + "*" * 5
*****
Pattern-24 — Right-Aligned Star Triangle with Spaces
Pattern-24 is similar to Pattern-23, but every star is followed by a space.
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Pattern-24 — Complete Program
Pattern-24 — Program Explanation
First, spaces are printed:
print(" "*(n-i),end="")
Then the inner loop prints stars:
for j in range(1,i+1):
print("*",end=" ")
The number of stars equals i.
Spaces = n - i Stars = i
Pattern-23 vs Pattern-24
| Pattern | Star Logic | Appearance |
|---|---|---|
| 23 | "*"*i |
Stars joined together |
| 24 | print("*",end=" ") |
Space after every star |
Pattern-23 → ***** Pattern-24 → * * * * *
Pattern-25 — Right-Aligned Repeated Number Triangle
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
The row number is repeated according to the current row.
Row 1 → 1 once Row 2 → 2 twice Row 3 → 3 three times ...
Pattern-25 — Complete Program
Pattern-25 — Program Explanation
Leading spaces:
" " * (n-i)
The number string is:
str(i) + " "
It is repeated i times:
(str(i)+" ") * i
For example:
i = 3 str(i) + " " → "3 " "3 " * 3 → "3 3 3 "
Pattern-26 — Right-Aligned Increasing Number Triangle
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Pattern-26 — Complete Program
Pattern-26 — Program Explanation
The leading-space formula remains:
n - i
The inner loop is:
range(1,i+1)
and prints:
j
Therefore:
i = 1 → 1 i = 2 → 1 2 i = 3 → 1 2 3 i = 4 → 1 2 3 4
The pattern is the right-aligned version of Pattern-11.
Pattern-27 — Right-Aligned Repeated Alphabet Triangle
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G
H H H H H H H H
I I I I I I I I I
J J J J J J J J J J
Pattern-27 — Complete Program
Pattern-27 — Program Explanation
The alphabet depends on the row:
chr(64+i)
Therefore:
i = 1 → A i = 2 → B i = 3 → C ...
The expression:
(chr(64+i)+" ")*i
repeats the current row alphabet i times.
i = 3
chr(67) = C
("C" + " ") * 3
C C C
Pattern-28 — Right-Aligned Increasing Alphabet Triangle
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
Pattern-28 — Complete Program
Pattern-28 — Program Explanation
Leading spaces:
" " * (n-i)
Number of characters:
i
The alphabet depends on j:
chr(64+j)
Therefore:
j = 1 → A j = 2 → B j = 3 → C j = 4 → D
For i = 4:
A B C D
Pattern-28 is the right-aligned version of Pattern-13.
Pattern-29 — Right-Shifted Decreasing Star Triangle
Pattern-29 reverses the direction used in the previous right-aligned patterns.
* * * * *
* * * *
* * *
* *
*
Now:
Spaces increase Stars decrease
Pattern-29 — Complete Program
Pattern-29 — Program Explanation
The leading-space formula is:
i - 1
The star-count formula is:
n + 1 - i
For n = 5:
| i | Spaces | Stars |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| 5 | 4 | 1 |
Therefore:
Spaces = i - 1 Stars = n + 1 - i
Pattern-29 — Dry Run with n = 5
i = 1 spaces = 0 stars = 5 → * * * * * i = 2 spaces = 1 stars = 4 → * * * * i = 3 spaces = 2 stars = 3 → * * * i = 4 spaces = 3 stars = 2 → * * i = 5 spaces = 4 stars = 1 → *
Pattern-30 — Right-Shifted Decreasing Repeated Number Triangle
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
The number decreases from n to 1.
The number of repetitions also decreases.
Pattern-30 — Complete Program
Pattern-30 — Program Explanation
The leading spaces are:
i - 1
The current number is:
n + 1 - i
The same formula determines how many times the number is repeated:
(str(n+1-i)+" ")*(n+1-i)
For n = 5:
i = 1
Number = 5 + 1 - 1
= 5
Repetitions = 5
5 5 5 5 5
i = 2
Number = 5 + 1 - 2
= 4
Repetitions = 4
4 4 4 4
i = 5
Number = 5 + 1 - 5
= 1
Repetitions = 1
1
Pattern-23 to Pattern-28 — Common Structure
Patterns 23–28 follow the same basic right-aligned structure.
for i in range(1,n+1):
print(" "*(n-i),end="")
# Print i values
The formulas are:
Spaces = n - i Values = i
Only the value being printed changes.
| Pattern | Printed Value |
|---|---|
| 23 | Star without spacing |
| 24 | Star with spacing |
| 25 | Current row number i |
| 26 | Column number j |
| 27 | chr(64+i) |
| 28 | chr(64+j) |
Pattern-29 and Pattern-30 — Common Structure
Patterns 29–30 use the opposite movement.
Spaces = i - 1 Values = n + 1 - i
Therefore:
Row increases
│
├── Spaces increase
│
└── Values decrease
| Row | Spaces | Values |
|---|---|---|
| 1 | 0 | n |
| 2 | 1 | n-1 |
| 3 | 2 | n-2 |
| ... | ... | ... |
| n | n-1 | 1 |
Right-Aligned Increasing vs Decreasing Patterns
| Type | Spaces | Values |
|---|---|---|
| Increasing | n-i |
i |
| Decreasing | i-1 |
n+1-i |
Increasing
Spaces: 4 3 2 1 0 Values: 1 2 3 4 5
Decreasing
Spaces: 0 1 2 3 4 Values: 5 4 3 2 1
These two formulas are very important for solving many upcoming pattern programs.
Part 3 — Complete Formula Table
| Pattern | Space Logic | Value / Loop Logic |
|---|---|---|
| 21 | None | chr(64+n+1-i) |
| 22 | None | chr(65+n-j) |
| 23 | n-i |
"*"*i |
| 24 | n-i |
range(1,i+1) stars |
| 25 | n-i |
(str(i)+" ")*i |
| 26 | n-i |
j |
| 27 | n-i |
(chr(64+i)+" ")*i |
| 28 | n-i |
chr(64+j) |
| 29 | i-1 |
"* "*(n+1-i) |
| 30 | i-1 |
(str(n+1-i)+" ")*(n+1-i) |
Part 3 — Execution Flow
START
│
▼
Read n
│
▼
Outer Loop (i)
│
▼
Decide Pattern Type
│
┌────────────┴────────────┐
│ │
▼ ▼
Increasing Decreasing
Right-Aligned Right-Shifted
│ │
▼ ▼
Spaces = n-i Spaces = i-1
Values = i Values = n+1-i
│ │
└────────────┬────────────┘
│
▼
Print Spaces
│
▼
Print Pattern
│
┌─────────────┼─────────────┐
▼ ▼ ▼
Stars Numbers Alphabets
│ │ │
└─────────────┴─────────────┘
│
▼
Next Line
│
▼
Next Row
│
▼
END
Part 3 — Important Notes
" "*(n-i)creates decreasing leading spaces." "*(i-1)creates increasing leading spaces.- For an increasing right-aligned triangle, use
n-ispaces andivalues. - For a decreasing right-shifted triangle, use
i-1spaces andn+1-ivalues. chr(64+n+1-i)creates a decreasing row-based alphabet.chr(65+n-j)creates reverse alphabets inside a row.chr(64+i)creates an alphabet based on the row.chr(64+j)creates alphabets based on columns.- String multiplication can be used instead of an inner loop when the same value must be repeated.
end=""prevents Python from moving to the next line while constructing the current row.
Part 3 — Summary
| Pattern | Pattern Type | Main Concept |
|---|---|---|
| 21 | Decreasing alphabet | Repeated reverse row alphabet |
| 22 | Decreasing alphabet | Reverse alphabet sequence |
| 23 | Increasing right-aligned | Joined stars |
| 24 | Increasing right-aligned | Spaced stars |
| 25 | Increasing right-aligned | Repeated row number |
| 26 | Increasing right-aligned | Increasing numbers |
| 27 | Increasing right-aligned | Repeated row alphabet |
| 28 | Increasing right-aligned | Increasing alphabets |
| 29 | Decreasing right-shifted | Stars |
| 30 | Decreasing right-shifted | Repeated numbers |
Part 3 — Quick Revision
RIGHT-ALIGNED PATTERNS
│
┌──────────────┴──────────────┐
│ │
▼ ▼
Increasing Decreasing
│ │
▼ ▼
Spaces = n-i Spaces = i-1
Values = i Values = n+1-i
│ │
▼ ▼
Spaces decrease Spaces increase
Values increase Values decrease
│ │
└──────────────┬──────────────┘
│
▼
Choose What to Print
│
┌───────────┼───────────┐
▼ ▼ ▼
Stars Numbers Alphabets
│
▼
Pattern Completed
Introduction
Part 4 covers Pattern-31 to Pattern-40.
We will learn:
Pattern-31 → Right-Shifted Decreasing Number Sequence Pattern-32 → Right-Shifted Decreasing Repeated Alphabet Pattern-33 → Right-Shifted Decreasing Alphabet Sequence Pattern-34 → Star Pyramid Pattern-35 → Repeated Number Pyramid Pattern-36 → Repeated Alphabet Pyramid Pattern-37 → Odd Alphabet Pyramid Pattern-38 → Palindrome Alphabet Pyramid Pattern-39 → Reverse Odd Number Pyramid Pattern-40 → Increasing Alphabet Pyramid
This part introduces one very important pyramid formula:
Number of Values = 2 * i - 1
For five rows:
i = 1 → 1 value i = 2 → 3 values i = 3 → 5 values i = 4 → 7 values i = 5 → 9 values
Pattern-31 — Right-Shifted Decreasing Number Sequence
Pattern-31 prints numbers starting from 1, but the number of values decreases in every row.
Output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Two things happen row by row:
Leading Spaces → Increase Numbers → Decrease
Pattern-31 — Complete Program
Pattern-31 — Step-by-Step Explanation
Step 1 — Read Number of Rows
n=int(input("Enter the number of rows: "))
The value of n controls the total number of rows.
Step 2 — Outer Loop
for i in range(1,n+1):
The variable i represents the current row.
Step 3 — Print Leading Spaces
print(" "*(i-1),end="")
The number of leading spaces is:
i - 1
Step 4 — Print Numbers
for j in range(1,n+2-i):
print(j,end=" ")
The inner loop prints:
1 to n + 1 - i
For n = 5:
i = 1 → 1 2 3 4 5 i = 2 → 1 2 3 4 i = 3 → 1 2 3 i = 4 → 1 2 i = 5 → 1
Pattern-31 — Dry Run
| i | Spaces | j Range | Output |
|---|---|---|---|
| 1 | 0 | 1 to 5 | 1 2 3 4 5 |
| 2 | 1 | 1 to 4 | 1 2 3 4 |
| 3 | 2 | 1 to 3 | 1 2 3 |
| 4 | 3 | 1 to 2 | 1 2 |
| 5 | 4 | 1 to 1 | 1 |
Pattern-32 — Right-Shifted Decreasing Repeated Alphabet
Output
E E E E E
D D D D
C C C
B B
A
The alphabet and its repetition count both decrease in every row.
Pattern-32 — Complete Program
Pattern-32 — Step-by-Step Explanation
The leading spaces are generated using:
" " * (i-1)
The current alphabet is calculated using:
chr(65+n-i)
For n = 5:
i = 1 → chr(69) → E i = 2 → chr(68) → D i = 3 → chr(67) → C i = 4 → chr(66) → B i = 5 → chr(65) → A
The repetition count is:
n + 1 - i
Therefore:
E → 5 times D → 4 times C → 3 times B → 2 times A → 1 time
Pattern-32 — Formula
Leading Spaces = i - 1 Alphabet = chr(65 + n - i) Repetitions = n + 1 - i
Pattern-33 — Right-Shifted Decreasing Alphabet Sequence
Output
A B C D E
A B C D
A B C
A B
A
Every row starts from A.
The ending alphabet decreases in every next row.
Pattern-33 — Complete Program
Pattern-33 — Step-by-Step Explanation
First, leading spaces are printed:
" " * (i-1)
The inner loop starts from:
65
ASCII value 65 represents:
A
The loop is:
range(65,66+n-i)
For n = 5:
Row 1 → ASCII 65 to 69
→ A B C D E
Row 2 → ASCII 65 to 68
→ A B C D
Row 3 → ASCII 65 to 67
→ A B C
Row 4 → A B
Row 5 → A
Pattern-31 to Pattern-33 — Common Logic
Patterns 31–33 follow the same basic shape.
Values decrease Spaces increase
The common formulas are:
Spaces = i - 1 Values = n + 1 - i
| Pattern | Printed Value |
|---|---|
| 31 | Increasing numbers from 1 |
| 32 | Repeated decreasing alphabet |
| 33 | Increasing alphabet sequence from A |
- Right-aligned patterns print leading spaces before the shape
- The number of leading spaces shrinks as the row grows
- Repeated patterns print the same value; sequence patterns print changing values
- chr(65 + row) generates alphabets that depend on the row