6

I'm struggling to understand this 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 prints an empty square. I thought I could use the code

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

to print the units in between the upper and the lower side of the square but they won't be aligned to the upper/lower side ones, which doesn't happen if you run the first code.

So, could this be because of end='' or print()?

Would you be so kind and tell me what do they mean?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Pldx
  • 87
  • 1
  • 3
  • 6
  • This are named or keyword parameters: http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html – PeterMmm Dec 04 '13 at 10:18
  • 1
    Possible duplicate : http://stackoverflow.com/questions/2456148/python-print-end – Nilani Algiriyage Dec 04 '13 at 10:26
  • @NilaniAlgiriyage I've also found that question and I don't think it's a duplicate. The question you found is mainly discussing the difference between `Python2` and `Python3` since there is no argument `end` for `print` in `Python2` (actually in `Python2` `print` is not a function but a statement). And this OP is trying to know what `end` does. – starrify Dec 04 '13 at 10:37

6 Answers6

9

Check the reference page of print. By default there is a newline character appended to the item being printed (end='\n'), and end='' is used to make it printed on the same line.

And print() prints an empty newline, which is necessary to keep on printing on the next line.

EDITED: added an example.
Actually you could also use this:

def a(n):
    print('*' * n)
    for i in range(n - 2):
        print('*' + ' ' * (n - 2) + '*')
    if n > 1:
        print('*' * n) 
starrify
  • 14,307
  • 5
  • 33
  • 50
  • Or, in a single call to `print`: `print( '*' * n + '\n' + ( '*' + ' '*(n-2) + '*\n' )*(n-2) + '*'*n )` – Robᵩ Dec 04 '13 at 14:15
  • @Robᵩ Of course there're many ways to write the code short, however this is not something like IOCCC and the purpose of source code is for people to read. I don't see any benefit to write like that. Also, are you sure you're providing a _correct_ example? You may try `n=1`. – starrify Dec 04 '13 at 14:30
1

In Python 3.x, the end=' ' is used to place a space after the displayed string instead of a newline.

please refer this for a further explanation.

Community
  • 1
  • 1
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
0

print() uses some separator when it has more than one parameter. In your code you have 3 ("" is first, ''(n-2) - second, "*" -third). If you don't want to use separator between them add sep='' as key-word parameter.

print("*", ' '*(n-2), "*", sep='') 
Andrey Shokhin
  • 11,250
  • 1
  • 16
  • 15
0
spam = ['apples', 'bananas', 'tofu', 'cats']
    i = 0
    for i in range(len (spam)):
        if i == len(spam) -1:
            print ('and', spam[i])
        elif i == len (spam) -2:
            print (spam [i], end=' ')
        else:
            print (spam [i], end=', ')

So I'm new to this whole coding thing, but I came up with this code. It's probably not as sophisticated as the other stuff, but it does the job.

spam = ['apples', 'bananas', 'tofu', 'cats']    
def fruits():
    i = 0
    while i != len(spam):
        if len(spam) != i :
            print ('and', spam[i])
            i += 1   
 fruits()  

try this!

Community
  • 1
  • 1
0

use this to understand

for i in range(0,52):
    print(5*"fiof" ,end=" ")

just put different things here in end and also use with sep

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-2
print('\n'.join('*{}*'.format((' ' if 0<row<n-1 else '*')*n-2) for row in range(n)))
volcano
  • 3,578
  • 21
  • 28
  • It's absolutely sure if you could write code without testing -- as long as they are correct. However in this case it's not. There are currently two syntax errors to be fixed in your answer, and also another bug that your code doesn't handle the case when `n==1`. – starrify Dec 04 '13 at 10:41