Python Program to Print B Star Pattern

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

The below alphabet B star pattern example accepts the user-entered rows and the nested for loop iterates the rows. The Else If or elif condition is to print stars at a few positions, get the Alphabet B pattern of stars, and skip others.

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

print("====The Alphabet B Star Pattern====")

for i in range(rows):
    print('*', end='')
    for j in range(2 * rows - 1):
        if (i == 0 or i == rows - 1 or i == rows // 2) and j < (2 * rows) - 2:
            print('*', end='')
        elif j == (2 * rows) - 2 and not (i == 0 or i == rows - 1 or i == rows // 2):
            print('*', end='')
        else:
            print(end=' ')
    print()
Enter Alphabet B of Stars Rows = 7
====The Alphabet B Star Pattern====
************* 
*            *
*            *
************* 
*            *
*            *
************* 

The above generates the Alphabet B, but you can also try the code below; it looks better than the above.

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

print("====The Alphabet B Star Pattern====")

n = rows // 2
for i in range(rows):
    print('*', end='')
    for j in range(n + 1):
        if (i == 0 or i == rows - 1 or i == n) and j < n:
            print('*', end='')
        elif j == n and not (i == 0 or i == rows - 1 or i == n):
            print('*', end='')
        else:
            print(end=' ')
    print()
Python Program to Print B Star Pattern

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

Instead of a For loop, this program uses the while loop to iterate the B pattern rows and prints the stars at each position. For more Star Pattern programs >> Click Here.

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

n = rows // 2
i = 0
while i < rows:
    print('*', end='')
    j = 0
    while j <= n:
        if (i == 0 or i == rows - 1 or i == n) and j < n:
            print('*', end='')
        elif j == n and not (i == 0 or i == rows - 1 or i == n):
            print('*', end='')
        else:
            print(end=' ')
        j = j + 1
    print()
    i = i + 1
Enter Alphabet B of Stars Rows = 10
****** 
*     *
*     *
*     *
*     *
****** 
*     *
*     *
*     *
****** 

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

def BPattern(rows, ch):
    n = rows // 2
    for i in range(rows):
        print('%c' %ch, end='')
        for j in range(n + 1):
            if (i == 0 or i == rows - 1 or i == n) and j < n:
                print('%c' %ch, end='')
            elif j == n and not (i == 0 or i == rows - 1 or i == n):
                print('%c' %ch, end='')
            else:
                print(end=' ')
        print()


row = int(input("Enter Alphabet B of Stars Rows = "))
sym = input("Symbol for B Pattern = ")
BPattern(row, sym)
Enter Alphabet B of Stars Rows = 9
Symbol for B Pattern = @
@@@@@ 
@    @
@    @
@    @
@@@@@ 
@    @
@    @
@    @
@@@@@