3

I'm trying to figure out how to turn my whole square into a hollow one. The few things I've tried so far haven't been very successful as I end up getting presented with a rather distorted triangle!

This is the code I have to form my square currently ..

size = 5
for i in range(size):
    print ('*' * size)

When run, this is the result ..

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

Do I need to run if or while statements when size is greater than 3 to specify a condition?

Ergo
  • 393
  • 1
  • 3
  • 9
  • 1
    Seems we can start a library of code to print all those shapes of asterisks used as beginner's exercise: [Pyramid](http://stackoverflow.com/questions/33179423/upside-down-pyramid-py), [M](http://stackoverflow.com/questions/28394149/draw-an-m-shaped-pattern-with-nested-loops), [Triangels](http://stackoverflow.com/questions/26352412/python-print-a-triangular-pattern-of-asterisks), [Diamond](http://stackoverflow.com/questions/31364162/print-shape-in-python), [Hollow square](http://stackoverflow.com/questions/16108446/drawing-a-hollow-asterisk-square) – cfi Oct 16 '15 at 21:17

10 Answers10

8

I think this is what you want to do:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [0, n-1] or j in [0, m-1] else ' ', end='')
    print()

Output:

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

You can also draw a triangle this way:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [j, m-1] or j == 0 else ' ', end='')
    print()

Output:

*         
**        
* *       
*  *      
*   *     
*    *    
*     *   
*      *  
*       * 
**********
aldeb
  • 6,588
  • 5
  • 25
  • 48
3

You can print out a single '*', followed by size-2 spaces, then a single '*'. This will give you the "hollow" portion. The first and last lines need the full length:

size = 5
inner_size = size - 2
print ('*' * size)
for i in range(inner_size):
    print ('*' + ' ' * inner_size + '*')
print ('*' * size)
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

Here is my python code for drawing a square by entered size N.

n = int(input())
print('*' * n)
for i in range(n-2):
    print ('*' + ' ' * (n-2) + '*')
print('*' * n)

Basically the first and the last print('*' * n) are drawing the top and the bottom lines and the for cycle prints the body.

Output example: N=3

***
* *
***

Output example: N=5

*****
*   *
*   *
*   *
*****
ahmedov
  • 21
  • 3
1

Try this:

size = 5
for i in range(1,size+1):
    if (i==1 or i==size):
        print("*"*size)
    else:
        print("*"+" "*(size-2),end="")
        print("*")

output:

*****
*   *
*   *
*   *
*****
user9093127
  • 181
  • 2
  • 3
  • 12
0

Heres my example:

print("Enter width")
width = int(input())
print("Enter height")
height = int(input())

for i in range(height):
    if i in[0]:
        print("* "*(width))
    elif i in[(height-1)]:
        print("* "*(width))
    else:
        print("*"+"  "*(width-2)+" *")

input()

Output: Image link

Hope this helps everyone else, who wants to leave spaces between asteriks, while printing a rectangle and if there are any mistakes in my code let me know, because I'm a beginner myself.

Mikk
  • 1
0
size = int(input('Enter the size of square you want to print = '))
for i in range(size):           # This defines the rows
    for j in range(size):       # This defines the columns
        print('*' , end=' ')    # Printing * and " end=' ' " is giving space      after every * preventing from changing line
    print()                     # Giving a command to change row after every column in a row is done

print()                         # Leaving one line
for k in range(size):           # This defines the rows
    print('* ' *size)           # Defines how many times * should be multiplied
pacholik
  • 8,607
  • 9
  • 43
  • 55
Daku
  • 1
0

Here is my python 3.6 code for drawing a square using column size and row size inputs:

column = int(input("Enter column size : "))
row = int(input("Enter row size : "))
print('* ' * column)
print(('* ' + "  " * (column-2)+ '*'+'\n')*(row -2) + ('* ' * column))
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Vicky Kumar
  • 9
  • 1
  • 3
0

You can also draw a triangle using this simple way:

n = int(input("enter num")) for i in range (n+1):

if 2>=i :
    print (i * "*")
elif n == i:
    print ("*" * i)
else:
    print ("*"+" "*(i-2)+"*")
Vicky Kumar
  • 9
  • 1
  • 3
0

Nice python library that will do this...

termshape - https://github.com/zvibazak/termshape

from termshape import get_rectangle 
print(get_rectangle(10, 5))
* * * * * * * * * *
*                 *
*                 *
*                 *
* * * * * * * * * *
zvi
  • 3,677
  • 2
  • 30
  • 48
0

In this code, there are 2 for loops and this loops will create edges. And "else statements" represents if our point is not on the edge code will print empty character(" "). "i == 0" shows upper edge and "i == length - 1" shows bottom edge. "j == 0" will be left edge and I think you can guess what is for "j == length - 1" and "print()".

def emptySquare(length,char):
    for i in range(length):
        for j in range(length):
            if (i == 0 or i == length-1 or j == 0 or j == length-1):
                print(char,end=" ")
            else:
                print(" ",end=" ")
        print()
Mehmet
  • 13
  • 4
  • 1
    While this might answer the question, if possible you should [edit] your answer to include a short explanation of *how* this code block answers the question. This helps to provide context and makes your answer much more useful to future readers. – Hoppeduppeanut Dec 02 '20 at 02:45
  • Okay, I will be more careful. – Mehmet Jan 04 '21 at 16:06