Home ยป Display a Sandglass Star Pattern using python

Display a Sandglass Star Pattern using python

Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification
Spring Framework Basics Video Course
1 Year Subscription

In this article we display a sandglass pattern using python

Table of Contents

Code

rows = int(input("Enter amount of Rows = "))

print("====Pattern====")

for i in range(0, rows):
    for j in range(0, i):
        print(end = ' ')
    for k in range(i, rows):
        print('*', end = ' ')                
    print()

for i in range(rows - 1, -1, -1):
    for j in range(0, i):
        print(end = ' ')
    for k in range(i, rows):
        print('*', end = ' ')      
    print()

When you run this you would see something like this

Alternative

def drawSandglass(rows, ch):
    for i in range(0, rows):
        for j in range(0, i):
            print(end = ' ')
        for k in range(i, rows):
            print('%c' %ch, end = ' ')               
        print()

    for i in range(rows - 1, -1, -1):
        for j in range(0, i):
            print(end = ' ')
        for k in range(i, rows):
            print('%c' %ch, end = ' ')     
        print()
    
rows = int(input("Enter amount of Rows = "))

char = input("Symbol to use = " )

print("====Pattern====")
drawSandglass(rows, char)

 

Link

sandglasspattern1

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More