-2

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

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

  • 6
    Read the documentation please. Or use an IDE that will show the actual doc strings whne you hover over the thing you're using. https://docs.python.org/3/library/functions.html#print the very first line explains it: "Print objects to the text stream file, separated by `sep` and followed by `end`. `sep`, `end`, `file`, and `flush`, if present, must be given as keyword arguments." Please do not use StackOverflow as a substitute for very basic web searches, or for the official documentation – Random Davis Mar 10 '23 at 20:29
  • 1
    That is a keyword argument being passed to the `print` function. – juanpa.arrivillaga Mar 10 '23 at 20:32
  • @CasperKuethe StackOverflow wouldn't be as popular in search engine rankings as it is today if it didn't take an aggressive stance on low quality posts. Nothing I said was mean, it's just not sugarcoated. ETA: besides, someone's experience as a programmer has no impact on whether they can follow rules, be polite, make sure they're following guidelines, etc. – Random Davis Mar 10 '23 at 20:33
  • google: "python print without newline" => https://stackoverflow.com/questions/493386/how-to-print-without-a-newline-or-space – Jean-François Fabre Mar 10 '23 at 20:35
  • @CasperKuethe I was helpful though, I provided all the info that was needed. – Random Davis Mar 10 '23 at 20:44
  • @maddes8cht this post specifically will not show up because it's a duplicate of a much more popular one; if something has an existing answer, watering down the site with duplicate posts with lower-quality answers is not the solution to help the site's SEO rankings. – Random Davis Mar 10 '23 at 20:45

2 Answers2

4

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.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
Casper Kuethe
  • 1,070
  • 8
  • 13
1

It means do not start a new line at the end of the print

By default, Python puts a newline character at the end of a print statement.

If you want, instead, to keep the output cursor on the same line, so you can print something afterwards, use end="". The next print statement will continue outputting on the same line.

print("Hello, ")
print("World!")

gives you

Hello,
World!

But

print("Hello, ", end="")
print("World!")

gives you

Hello, World!

Here is my evidence.

enter image description here

E_net4
  • 27,810
  • 13
  • 101
  • 139
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • 1
    Its meaning is more general than you imply. The value of the `end` parameter is just the string that gets prints after all the arguments. Its *default* value happens to be `'\n'`. Changing it implies that no new line is started *only* if the new value doesn't contain a `\n'`. – chepner Mar 10 '23 at 20:49