Instagram
youtube
Facebook
Twitter

Python Program to Print Star-Hyphen Combination Pattern - 2

A Python Program to Print Star-Hyphen Combination Pattern - 2?

Code Explanation :
● Loop Range:
The pattern is built using two loops: one for the upper half (1 to n) and one for the lower half (n-1 to 1).

● Character Selection:
If the row number is odd, it prints *, otherwise -, using char = '*' if i % 2 != 0 else '-'.

● Repetition for Row:
Each row prints the selected character i times, with a space after each.

● Left-Aligned Shape:
Unlike center-aligned diamond, this pattern starts from the left, making it look like a triangle pointing right.

● Symmetric Design:
Top and bottom loops ensure symmetry vertically.


Program:

n = 5  

# Upper half

for i in range(1, n + 1):

    char = '*' if i % 2 != 0 else '-'

    print((char + " ") * i)

# Lower half

for i in range(n - 1, 0, -1):

    char = '*' if i % 2 != 0 else '-'

    print((char + " ") * i)