Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

https://www.hackerrank.com/challenges/python-print/problem?isFullScreen=true

here is the Link of the Problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I did reach this point and got stuck:

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

The output should be 123…n.

My Output was
1
2
3
.
.
.
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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading