'Printing Simple Diamond Pattern in Python

I would like to print the following pattern in Python 3.5 (I'm new to coding):

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

But I only know how to print the following using the code below, but not sure how to invert it to make it a complete diamond:

n = 5

print("Pattern 1")

for a1 in range (0,n):
    for a2 in range (a1):
        print("*", end="")
    print()

for a1 in range (n,0,-1):
    for a2 in range (a1):
        print("*", end="")
    print()

*
**
***
****
*****
****
***
**
*

Any help would be appreciated!



Solution 1:[1]

Here is a solution base on height equals to top to the middle, or half of the height. For example, height is entered as 4(7) or 5(9) below. This method will yield odd number actual height

h = int(input("please enter diamond's height:"))

for i in range(h):
    print(" "*(h-i), "*"*(i*2+1))
for i in range(h-2, -1, -1):
    print(" "*(h-i), "*"*(i*2+1))

# please enter diamond's height:4
#      *
#     ***
#    *****
#   *******
#    *****
#     ***
#      *
#
# 3, 2, 1, 0, 1, 2, 3  space
# 1, 3, 5, 7, 5, 3, 1  star

# please enter diamond's height:5
#       *
#      ***
#     *****
#    *******
#   *********
#    *******
#     *****
#      ***
#       *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4  space
# 1, 3, 5, 7, 9, 7, 5, 3, 1  star

Here is another solution base on height equals to top to the bottom, or the actual total height. For example, height is entered as 7 or 9 below. When the user enters an even number for height, the diamond will be slightly slanted.

h = int(input("please enter diamond's height:"))

for i in range(1, h, 2):
    print(" "*(h//2-i//2), "*"*i)
for i in range(h, 0, -2):
    print(" "*(h//2-i//2), "*"*i)

# please enter diamond's height:7
#      *
#     ***
#    *****
#   *******
#    *****
#     ***
#      *
#
# 3, 2, 1, 0, 1, 2, 3  space
# 1, 3, 5, 7, 5, 3, 1  star
#
# please enter diamond's height:9
#       *
#      ***
#     *****
#    *******
#   *********
#    *******
#     *****
#      ***
#       *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4  space
# 1, 3, 5, 7, 9, 7, 5, 3, 1  star

Solution 2:[2]

As pointed out by Martin Evans in his post: https://stackoverflow.com/a/32613884/4779556 a possible solution to the diamond pattern could be:

side = int(input("Please input side length of diamond: "))

for x in list(range(side)) + list(reversed(range(side-1))):
    print('{: <{w1}}{:*<{w2}}'.format('', '', w1=side-x-1, w2=x*2+1))

Solution 3:[3]

I learned a very simple solution today and would like to share it. :)

num = 9

for i in range(1, num+1):
  i = i - (num//2 +1)
  if i < 0:
    i = -i
  print(" " * i + "*" * (num - i*2) + " "*i)

The logic is the following:
(A space is represented as "0" here.)

# i = 1 | new i = 1 - 5 = -4 | * : 9 - 8 = 1 | 0000 + * + 0000
# i = 2 | new i = 2 - 5 = -3 | * : 9 - 6 = 3 | 000 + *** + 000
# i = 3 | new i = 3 - 5 = -2 | * : 9 - 4 = 5 | 00 + ***** + 00
# i = 4 | new i = 4 - 5 = -1 | * : 9 - 2 = 7 | 0 + ******* + 0
# i = 5 | new i = 5 - 5 = 0  | * : 9 - 0 = 9 |    ********* 
# i = 6 | new i = 6 - 5 = 1  | * : 9 - 2 = 7 | 0 + ******* + 0
# i = 7 | new i = 7 - 5 = 2  | * : 9 - 4 = 5 | 00 + ***** + 00
# i = 8 | new i = 8 - 5 = 3  | * : 9 - 6 = 3 | 000 + *** + 000
# i = 9 | new i = 9 - 5 = 4  | * : 9 - 8 = 1 | 0000 + * + 0000

The result would be the following:

    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *    

Solution 4:[4]

Simplest Answer

n=5
for i in range(1,n+1):
    print ((n-i)*(" ")+(i*" *"))

for i in range(n-1,0,-1):
    print((n-i)*(" ")+(i*" *"))

Hope this helps some one

Solution 5:[5]

Another possibility. Depending on which (space or star) one uses, (I used space) convert it to absolute value. This implementation doesn't require splitting the diamond into two loops (upper and lower halves).

def diamond(n):
    star = 1
    main = ''
    # if required to manage zero or negative n
    if n%2 == 0:
        return None
    if n<0:
        return None
    else:
        for i in range(1,n+1):
            string = ''
            space = abs(i - int((n+1)/2))
            star = n - 2 * space
            string = space * ' ' + star * '*' + '\n'
            main += string
        # not necessary but useful to visualize diamond 
        #print(main)
        return(main)

Solution 6:[6]

There are two version of this

  1. Space between Stars
  2. Without Space between Stars

Space between Stars

n = 4
for i in range(n):
    print(' '*(n-i-1) + '* '*(i+1) )
for i in range(n):
    print(' '*(i+1) + '* '*(n-i-1))

enter image description here

Without Space between Stars

n = 4
for i in range(n):
    print(' '*(n-i-1) + '*'*((2*i)+1) )
for i in range(n):
    print(' '*(i+1) + '*'*((2*((n-1)-i))-1))

enter image description here

Solution 7:[7]

side = int(input("side length: "))
count = 0
bl = 0
while count < side:
    x = side - count
    print (x * " ", (count * "*") * 2)
    count += 2
while count >= 0:
    print (bl * " ", (count * "*") * 2)
    count -= 1
    bl += 1

if you want both upper part and down side to be same change the count += 2 to count += 1

Solution 8:[8]

#author Tahir Baku
#have fun
print "\nWelcome to diamond builder"
print "\n----D.I.A.M.O.N.D  B.U.I.L.D----"
diagonal=int(input("Give me the diagonal: "))
s=1
h1=1
h=(diagonal-1)/2
diagonal1=diagonal-2
while s<=diagonal:
     print (' '*h+'*'*s+' '*h)
     h=h-1
     s=s+2
while diagonal1>=0:
     print (' '*h1+'*'*diagonal1+' '*h1)
     h1=h1+1
     diagonal1=diagonal1-2

Solution 9:[9]

print('This is in python 3.7')
h=eval(input('Enter the diagonal?'))
j=1
for i in range(h,h//2,-1):
    print(' '*(i-(h//2)-1),'*'*j)
    j+=2
j-=4
for i in range(1,(h//2)+1,1):
    print(' '*i,'*'*(j))
    j-=2

Solution 10:[10]

simple way ...

n= 11 #input is even number 1,3,5,...
a = 1
b = 1
for a in range(n+1):
    if a%2 != 0:
        val = (n - a) // 2
        print (" "*val + "*"*(a) + " "*val ,end = "\n")
for b in range(n-1,0,-1):
    if b%2 != 0:
        val2 = (n-b)//2
        print (" "*val2 + "*"*(b) + " "*val2 ,end = "\n")

Output:

     *     
    ***    
   *****   
  *******  
 ********* 
***********
 ********* 
  *******  
   *****   
    ***    
     *  

Or using reverse method, first one is diamond, second one is series of diamond

import copy
n = 10       #input: size of diamon
raw  = []
lst = []
re_lst = []

for a in range(n+1):
    if a%2 != 0:
        val = (n - a) // 2
        raw = ''.join(" "*val + "*"*(a) + " "*val)
        lst.append(raw)
        re_lst = copy.deepcopy(lst)
lst.reverse()
#print diamond
for i in re_lst:
    print(i)
for i in lst:
    print(i)
print("\n")
#print series of diamond
for i in re_lst:
    print(i)
    for i in lst:
        print(i)

Solution 11:[11]

a = 10
for x in range (a):
  print(" " * (a - x) + "*" * (x+1) + "*" *(x))
#+ this = diamond
for x in reversed(range(a-1)):
    print(" " * (a - x) + "*" * (x) + "*" *(x+1))

Solution 12:[12]

def pattern2(row):
    s=1
    c = row / 2
    d = int(c)-1
    for i in range(1,row+1):
        if i<c:
            print(" "*d,star(s))
            s+=2
            d-=1
        elif i==(c+0.5):
            print(star(row))
            s=s-2
            d=0
        else:
            print(" "*d,star(s))
            s=s-2
            d+=1
def star(s):
    return '*'*s
def main():
    row=int(input("enter the no. of row but the rows should be odd \n#special case of pattern"))
    try:
        a=row%2
        assert a!=0 and row!=0
        pattern2(row)
    except:
        print('Worng selection of rows for the perfect diamond.')
if __name__=="__main__":
    main()

Solution 13:[13]

#maybe it could help  

height = eval ( input ( 'How high? ' ) )
height = int (height//2)

for i in range(1, height+1):
    print(' ' *(height-i+1), '*'*i + '*' * (i-1) )

for i in range (height+1, 0, -1):
    print (' ' * (height+1-i), '*' * i + '*' * (i-1))  

Solution 14:[14]

First find logic from the dressing table, then I think it's easier above all (n=11, m=(n+1)/2 = 6) : i    .   *
1   5   1
2   4   3
3   3   5
4   2   7
5   1   9
6   0   11
7   1   9
8   2   7
9   3   5
10  4   3
11  5   1 enter image description here

n = int(input("Input odd number of layers: "))
m = (n+1)/2
i = 1
while i<=n :
    if(i < m):
        b = m-i
        s = 2*i -1
    else:
        b = i-m
        s = 2*(n-i)+1
    j = 1 
    while j <= b:
        print('.',end="")    # dot or space 
        j += 1
    j = 1    
    while j <= s:
        print('*',end="")
        j+=1
    print()
    i += 1

Solution 15:[15]

h = int(input('Height -> '))

for i in range(h):
    if i <= (h // 2):
        print(' ' * (h // 2 - i), end='')
        print('*' * (i + 1), end='')
        print('*' * (i + 1))
    else:
        print(' ' * abs((h // 2 - i)), end='')
        print('*' * (h - i), end='')
        print('*' * (h - i))

Solution 16:[16]

length = 5
for i in range(1,length,2):
    space = length//2 -i//2
    print(" "*space, end="")
    print('*'*i)
for j in range(length, 0, -2):
    space = length//2 - j//2
    print(" "*space, end="")
    print('*'*j)

Solution 17:[17]

#coder_rishabh_darmwal
#it_is_a_simple_codewith_an_easy_logic
row=int(input('enter the no. of rows')
for i in range(row):
    if i<=row//2:
        for j in range(row//2-i):
            print(" ",end='')
        for k in range(i*2-1):
        print("*",end="")
    print()
else:
    for j in range(i-row//2):
        print(" ",end="")
    for k in range((row-i)*2-1):
        print("*",end="")
    print()
#the output will be
[output for row=30][1]


#i also wrote a programme fro hollow diamonds
row=int(input('enter the no. of rows')
for i in range(row):
    if i<=row//2:
        for j in range(row//2-i):
            print(" ",end='')
        for k in range(i*2-1):
            if k==0 or k==i*2-2:
                print("*",end="")
            else:
                print(' ',end='')
        print()
    else:
        for j in range(i-row//2):
            print(" ",end="")
        for k in range((row-i)*2-1):
            if k==0 or k==(row-i)*2-2:
                print("*",end="")
            else:
                print(' ',end="")
        print()
[out for hollow rhombus row=20
][2]


  [1]: https://i.stack.imgur.com/3j0bx.png
  [2]: https://i.stack.imgur.com/tCxI3.png