This article shows how to write a Python program to print the Alphabet J star pattern using the for loop, while loop, and functions with an example.
The below alphabet J star pattern example accepts the user-entered rows, and the nested for loop iterates the rows. The elif condition is to print stars at a few positions to get the Alphabetical J pattern of stars and skip others.
rows = int(input("Enter Alphabet J of Stars Rows = ")) print("====The Alphabet J Star Pattern====") for i in range(rows): for j in range(rows): if i == rows - 1 and (0 < j < rows - 1): print("*", end="") elif (j == rows - 1 and i != rows - 1) or (i > rows // 2 and j == 0 and i != rows - 1): print("*", end="") else: print(end=" ") print()
The Python code below uses the if else statement to print the Alphabet J pattern of stars in more straight lines.
rows = int(input("Enter Alphabet J of Stars Rows = ")) for i in range(rows): for j in range(rows): if (i == 0 or j == rows // 2) or (i == rows - 1 and j <= rows // 2): print("*", end="") else: print(end=" ") print()
Enter Alphabet J of Stars Rows = 13
*************
*
*
*
*
*
*
*
*
*
*
*
*******
Python program to print the Alphabet J Star pattern using while loop
Instead of a For loop, this program uses the while loop to iterate the Alphabet J pattern rows and prints the stars at each position. For more Star Pattern programs >> Click Here.
rows = int(input("Enter Alphabet J of Stars Rows = ")) i = 0 while i < rows: j = 0 while j < rows: if i == rows - 1 and (0 < j < rows - 1): print("*", end="") elif (j == rows - 1 and i != rows - 1) or (i > rows // 2 and j == 0 and i != rows - 1): print("*", end="") else: print(end=" ") j = j + 1 print() i += 1
Enter Alphabet J of Stars Rows = 9
*
*
*
*
*
* *
* *
* *
*******
In this Python example, we created a JPattern function that accepts the rows and the symbol or character to print the Alphabet J pattern of the given symbol.
def JPattern(rows, ch): for i in range(rows): for j in range(rows): if i == rows - 1 and (0 < j < rows - 1): print('%c' %ch, end='') elif (j == rows - 1 and i != rows - 1) or (i > rows // 2 and j == 0 and i != rows - 1): print('%c' %ch, end='') else: print(end=" ") print() row = int(input("Enter Alphabet J of Stars Rows = ")) sy = input("Symbol for J Star Pattern = ") JPattern(row, sy)
Enter Alphabet J of Stars Rows = 14
Symbol for J Star Pattern = @
@
@
@
@
@
@
@
@
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@