What Does [end=""] means python?


So I have been solving this problem on HackerRank and got stuck thinking about how to print the output of the problem on the same line. After that, I gave up and looked up the answer on the internet. Even though it worked all confused about how and why.


here is the Link of the Problem

I did reach this point and got stuck:

n = int(input())
for i in range(1, n+1):

The output should be 123…n.

My Output was
n(every number on a new line)

The output should be in the same line.
So here is the required soln to the problem

n = int(input())
for i in range(1, n+1):
    print(i, end="")

Can Anyone explain how it works? How did this , end="" changed the whole solution

>Solution :

python adds a \n’ (newline) character after each string if you use the print function. With the named argument end you can provide a custom last character, or a string, instead of ‘\n’. So if end="" there will be nothing added to the end resulting in everything printed on the same line.

Leave a Reply Cancel reply