How to print a box in python

I'm to "Write a python program that prompts the user to input a positive integer n. The program then prints a hollow rectangle with n rows and 2*n columns. For a example an input of 3 would output:"

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

And my code is:

n=int[input['Please enter a positive integer between 1 and 15: ']]
for col in range[n]:
    for row in range[n]:
        print['*' if col in[0,[2*n]+1] or row in[0,n+1] else ' ', end=' ']
    print[]

But my output doesn't look at all like what I need; it's like the upper and left half of a hollow box. In addition, I'm not getting double columns like I need. What am I doing wrong?

EDIT: Thank you everyone for your help! Gave me a lot of insight and was very helpful. I modified my code to the following, and it works very well:

>n=int[input['Please enter a positive integer between 1 and 15: ']]
>for row in range[n]:
>    for col in range[2*n]:
>        print['*' if row in[0,n-1] or col in[0,[2*n]-1] else ' ', end=' ']
>    print[]

Special shout out to user2357112; you made me realize exactly what I had tripped up.

Python program to print a hollow square pattern of star:

In this tutorial program, we will learn how to print a hollow square pattern in python. We will show you how to print that pattern using star [*], but you can also modify the program to print the pattern using any other character or symbol like &,%,$,#,@, etc.

To print the pattern, we can either use for loop or while loop. We will show you how to write the code using both ways. The final output will look as like below :

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

We will ask the user to enter the size of one side of the square. Since all sides are equal, we will write the program to read one side and print the hollow square using the user-provided size.

Print a hollow square using a for loop :

length = int[input["Enter the side of the square  : "]]

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['*', end = ' ']
        else:
            print[' ', end = ' ']
    print[]

Sample output :

Enter the side of the square  : 6
* * * * * *
*         *
*         *
*         *
*         *
* * * * * *

Explanation :

To learn how the program worked, let me change the print statement of the else statement as below :

length = int[input["Enter the side of the square  : "]]

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['*', end = ' ']
        else:
            print['$', end = ' ']
    print[]

Now, if you run the program, it will print a square of size like below :

Enter the side of the square  : 6
* * * * * *
* $ $ $ $ *
* $ $ $ $ *
* $ $ $ $ *
* $ $ $ $ *
* * * * * *

As you can see, the second print statement is actually used to print the blank spaces , in this example, we are using that print statement to print the $ symbol.

  • The outer print is used to print the ’*’ and the inner print is used to print the blank spaces.
  • The outer loop will run for length number of times. In this example, it runs for i = 0 to i = 6. The inner loop will also run for the same time , i.e. j = 0 to j = 6.
  • We are printing the * only if i == 0 , i == length - 1 , j == 0 or j == length - 1. i.e. we are printing symbol for the first row, last row, first column and last column. For other values, we are printing blank spaces.

I hope that you have got an understanding of how the program actually works and how it printed out the result. Now, let’s try to implement this using a while loop :

Print a hollow square using a while loop :

Same as the above example, we can also implement it using a while loop. Let me show you how :

length = int[input["Enter the side of the square  : "]]

row = 1

while[row  Say box into the diagram..
Double-click it to display the inner diagram..
Copy the Say text box..
Click the root label to come back to the behavior..
Paste the Say text box..
Delete the Say box..

What is print [' '] in Python?

The Python print[] function takes in python data such as ints and strings, and prints those values to standard out. To say that standard out is "text" here means a series of lines, where each line is a series of chars with a '\n' newline char marking the end of each line.

How do you print a hollow pattern in Python?

Output: Hollow Rectangular Pattern.
Print 1-10-101-1010 Number Pattern..
Print 0-01-010-0101 Binary Number Pattern..
Print 1-101-10101 Binary Number Pyramid..
1-10-101-1010 pattern..

Chủ Đề