Python Forum

Full Version: When using print what does end="" do ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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.
Try this
print(symbol, end="SPAM")
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.