Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm actually struggling to understand this kind of exercise.

def a(n):

    for i in range(n):

        for j in range(n):

            if i == 0 or i == n-1 or j == 0 or j == n-1:

                print('*',end='')

            else:

                print(' ',end='')

        print()

Which gives a output of empty square. I even thought of using this code

print("*", ''*(n-2),"*")

to print the units in the middle of the upper and the lower side of the square yet they will not be adjusted to the upper/lower side ones, which doesn't occur on the off chance that you run the principal/first code... so... could this be a direct result of end='' or print() (would you be so kind and mention to me their meaning could be a little clearer.).

1 Answer

0 votes
by (26.4k points)

Kindly check the reference page of the print. Of course there is a newline character affixed to the thing being printed (end='\n'), and end='' is utilized to make it imprinted on a similar line. 

Furthermore, print() prints a void/empty newline, which is important to continue printing on the following line.

You can also try using this code:

def a(n):

    print('*' * n)

    for i in range(n - 2):

        print('*' + ' ' * (n - 2) + '*')

    if n > 1:

        print('*' * n) 

Interested to learn python in detail? Come and Join the python course.

Related questions

Browse Categories

...