This article shows how to write a Python program to print the Alphabet V star pattern using the for loop, while loop, and functions with an example.
The below alphabet V 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 V pattern of stars and skip others.
rows = int(input("Enter Alphabet V of Stars Rows = ")) print("====The Alphabetical V Star Pattern====") for i in range(rows): for j in range(2 * rows): if (i + j == 2 * rows - 2 or i == j) and i < 2 * rows / 2: print("*", end=" ") else: print(" ", end=" ") print()
Python program to print the Alphabet V Star pattern using while loop
Instead of a For loop, this program uses the while loop to iterate the Alphabet V pattern rows and prints the stars at a few positions. For more Star Pattern programs >> Click Here.
rows = int(input("Enter Alphabet V of Stars Rows = ")) print("====The Alphabetical V Star Pattern====") i = 0 while i < rows: j = 0 while j < 2 * rows: if (i + j == 2 * rows - 2 or i == j) and i < 2 * rows / 2: print("*", end=" ") else: print(" ", end=" ") j += 1 print() i += 1
Enter Alphabet V of Stars Rows = 10
====The Alphabetical V Star Pattern====
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
In this Python example, we created a VPattern function that accepts the rows and the symbol or character to print the Alphabet V pattern of the given symbol.
def VPattern(rows, ch): for i in range(rows): for j in range(rows): if (i + j == rows - 1 and i < rows / 2) or (i == j and i < rows / 2): print('%c' %ch, end=' ') else: print(" ", end=" ") print() row = int(input("Enter Alphabet V of Stars Rows = ")) sy = input("Symbol for V Star Pattern = ") VPattern(row * 2 + 1, sy)
Enter Alphabet V of Stars Rows = 12
Symbol for V Star Pattern = #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#