Python Program to Print Y Star Pattern

This article shows how to write a Python program to print the Alphabet Y star pattern using the for loop, while loop, and functions with an example. 

The below alphabet Y star pattern example accepts the user-entered rows, and the nested for loop iterates the rows. The If else condition is to print stars at the required positions to get the Alphabetical Y pattern of stars and skip others.

rows = int(input("Enter Alphabet Y of Stars Rows = "))
print("====The Alphabetical Y Star Pattern====")

for i in range(rows):
    for j in range(rows):
        if (i + j == rows - 1) or (i == j and i < rows / 2):
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
Enter Alphabet Y of Stars Rows = 9
====The Alphabetical Y Star Pattern====
*               * 
  *           *   
    *       *     
      *   *       
        *         
      *           
    *             
  *               
*     

The above code prints the Alphabet Y pattern of stars, but this Python example is more better than the above.

rows = int(input("Enter Alphabet Y of Stars Rows = "))
print("====The Alphabetical Y Star Pattern====")

if rows % 2 == 0:
    rows += 1

for i in range(rows):
    for j in range(rows):
        if (i + j == rows - 1 or i == j) and (i < rows // 2) or (j == rows // 2 and i > rows // 2 - 1):
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
Python Program to Print Alphabetical Y Star Pattern

Python program to print the Alphabet Y Star pattern using while loop

Instead of a For loop, this program uses the while loop to iterate the Alphabet Y pattern rows and prints the stars at a few positions. For more Star Pattern programs >> Click Here

rows = int(input("Enter Alphabet Y of Stars Rows = "))

if rows % 2 == 0:
    rows += 1

i = 0
while i < rows:
    j = 0
    while j < rows:
        if (i + j == rows - 1 or i == j) and (i < rows // 2) or (j == rows // 2 and i > rows // 2 - 1):
            print("*", end=" ")
        else:
            print(" ", end=" ")
        j += 1
    print()
    i += 1
Enter Alphabet Y of Stars Rows = 11
*                   * 
  *               *   
    *           *     
      *       *       
        *   *         
          *           
          *           
          *           
          *           
          *           
          * 

In this Python example, we created a YPattern function that accepts the rows and the symbol or character to print the Alphabet Y pattern of the given symbol.

def YPattern(rows, ch):

    for i in range(rows):
        for j in range(rows):
            if (i + j == rows - 1 or i == j) and (i < rows // 2) or (j == rows // 2 and i > rows // 2 - 1):
                print('%c' %ch, end=' ')
            else:
                print(" ", end=" ")
        print()


row = int(input("Enter Alphabet Y of Stars Rows = "))
sy = input("Symbol for Y Star Pattern = ")

if row % 2 == 0:
    row += 1
YPattern(row, sy)
Enter Alphabet Y of Stars Rows = 14
Symbol for Y Star Pattern = #
#                           # 
  #                       #   
    #                   #     
      #               #       
        #           #         
          #       #           
            #   #             
              #               
              #               
              #               
              #               
              #               
              #               
              #               
              #