What is the purpose of the end parameter in Python’s print() function, especially in loops? [duplicate]

5 days ago 4
ARTICLE AD BOX

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)

answered Dec 4, 2013 at 10:20

starrify's user avatar

2 Comments

Or, in a single call to print: print( '*' * n + '\n' + ( '*' + ' '*(n-2) + '*\n' )*(n-2) + '*'*n )

2013-12-04T14:15:34.037Z+00:00

@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.

2013-12-04T14:30:41.887Z+00:00

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's user avatar

answered Dec 4, 2013 at 10:25

Nilani Algiriyage's user avatar

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='')

answered Dec 4, 2013 at 10:25

Andrey Shokhin's user avatar

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's user avatar

answered Sep 27, 2017 at 14:52

user8683955's user avatar

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's user avatar

Suraj Rao

29.7k11 gold badges97 silver badges104 bronze badges

answered Sep 21, 2021 at 10:19

animesh negi's user avatar

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

answered Mar 6, 2024 at 9:02

J Sujal Kumar's user avatar

3 Comments

@MattSeymour: Can you explain why this isn’t an answer?

2024-03-07T00:15:55.39Z+00:00

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

2024-03-07T00:16:19.017Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article