Python Program for Printing Hollow Pyramid Star Pattern
Print Hollow Pyramid Star Pattern
Lets have a look at the program to hollow pyramid star pattern in python
We are going to discuss two methods below –
Method 1
Run
row = 8 for i in range(row): for j in range(row - i): # prints left space in the same row print(' ', end='') for j in range(2 * i + 1): # printing the left boundary of triangle if j == 0: print('*', end='') # printing the right boundary of triangle elif j == 2 * i: print('*', end='') # printing the bottom boundary of triangle elif i == row - 1: print('*', end='') # printing spaces in the middle of the triangle else: print(' ', end='') print() # printing new line
Output
* * * * * * * * * * * * * ***************
Method 1.1
The above method can also be condensed down from multiple else if conditions to single as shown below –
Run
row = 8 for i in range(row): for j in range(row - i): # prints left space in the same row print(' ', end='') for j in range(2 * i + 1): # printing the left boundary of triangle if j == 0 or j == 2 * i or i == row - 1: print('*', end='') # printing spaces in the middle of the triangle else: print(' ', end='') print() # printing new line
Output
* * * * * * * * * * * * * ***************
Method 2
Run
num = 8 k = 0 for i in range(1, num + 1): # printing left spaces in rows for j in range(i, num): print(" ", end="") while k != (2 * i - 1): # printing left/right boundaries if k == 0 or k == 2 * i - 2: print("*", end="") # printing spaces in the middle of the triangle else: print(" ", end="") k = k + 1 k = 0 print() # printing bottom boundary of the triangle for i in range(0, 2 * num - 1): print("*", end="")
Output
* * * * * * * * * * * * * ***************
#python
n=int(input())
sum=1
for i in range(n):
if(i==0 or i==n-1):
print(” “*(n-1-i)+”*”*sum)
else:
print(” “*(n-1-i)+”*”+” “*(sum-2)+”*”)
sum=sum+2
Hey! You can join our Discord Server for your queries, Our expert team of mentors would be guiding you there. 😊
TRY THIS, IT’S WAY EASIER:
n = int(input(‘enter a number: ‘))
for i in range(0, n):
if i == 0:
print(‘ ‘*(n-1) + ‘*’)
elif i == n-1:
print(‘*’*(2*i+1))
else:
print(‘ ‘*(n-1-i) + ‘*’ + ‘ ‘*((i-1)*2+1) +’*’)
n=5
f=5*2-1
for i in range(0,n):
if(i==0) or (i==n-1):
print(” ” * (1*i) , “$” * (f-2*i) , sep=””)
else:
print(” “*(1*i),”$”,” “*(f-(2*i)-2),”$”,sep=””)
n=n+1
for i in range(n,-1,-2):
print(” “,end=””)
print(” “*(i//2),end=””)
for p in range(0,n-i+1):
if(p==0):
print(“*”,end=””)
elif(p==n-i):
print(“*”,end=””)
else:
print(” “,end=””)
print(” “*(i//2),end=””)
print()
print(“*”*(n+3),end=””)
inp = int(input())
j = 0
for i in range(inp):
if i == 0:
print( ((inp-i)*’ ‘) +’*’)
elif i == inp -1:
print( ((inp-i)*’ ‘) +( (i+1) + (j+1) ) * ‘*’)
else:
print( ((inp-i)*’ ‘) +’*’+ (i + j) * ‘ ‘ +’*’)
j = j + 1
n=int(input(“Entert the number: “))
for i in range(n):
for j in range(n-i):
print(” “,end=””)
for k in range(2*i+1):
# Condition of the if function
if k == 0 or k == 2 * i or i == n – 1:
print(“*”,end=””)
else:
print(” “,end=””)
print()
n = int(input(“Enter the number:”))
for i in range(n):
if ((i==0) or (i==n-1)):
print(” “*(n-i) + (“*”)*(2*i+1))
else:
print(” “*(n-i) + (“*”) + (” “)*(2*i -1) + (“*”))
can solve even simply
num = int(input(“Enter the Number: “))
for i in range(0,num):
if i==0 or i==num-1:
print(” “*(num-i-1)+”*”*(i*2+1))
else:
print(” “*(num-i-1)+”*”+” “*(i*2+1-2)+”*”)
num = int(input(“enter number:”))
for i in range(1, num):
for j in range(0, num-i):
print(” “, end = ” “)
for j in range(0, (i*2)-1):
if((i > 0 and i 0 and j < (i*2)-2)):
print(" ",end = " ")
else:
print("*",end =" ")
print()