Python Forum
When using print what does end="" do ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When using print what does end="" do ?
#1
Hi everyone, i just started learning python so this might sound like a very stupid question.

So i'm doing this exercise about nested loop and i came across a line of code which i don't understand it's purpose.
What i don't understand is why ( end="" ) will actually make this loop work by show

Output:
333 333 333
However when i do not put ( end=""), it actually shows

Output:
3 3 3 3 3 3 3 3 3
So what i want to know is that what does this line of code does ( end="")

Thank you!

rows = int(input("How many rows? "))
columns = int(input("How many columns? "))
symbol = input("What symbol do you want? ")

for i in range(rows):
    for j in range(columns):
        print(symbol, end="") # what does end="" do ?
    print()
Yoriz write Oct-17-2021, 08:51 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The end parameter to print specifies the string that should be printed after all of the other arguments you pass to the function. The default (i.e. when you don't pass a value) is to print a new line character.
Reply
#3
Try this
print(symbol, end="SPAM")
Reply
#4
The other related argument is sep. Sep is what is printed between each item in the print statement. So, if you want commas you add "sep = ', '".

More common these days is to use f strings which allow a better set of formatting options. But, good to know about sep and end as they work with older versions of Python as well.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020