Prabodi Senevirathna Prabodi Senevirathna - 1 month ago 5
Python Question

How to use new line in Python?

I'm using the following code:

for x in range(8):
print(x,end=" ") #Loop run for 8 times(from 0 to 7)

print('\n')

for x in range(1,8):
print(x,end=" ")


When I run it, the output is:

0 1 2 3 4 5 6 7

1 2 3 4 5 6 7


I only need one new line in the answer; that is, I want the output as:

0 1 2 3 4 5 6 7
1 2 3 4 5 6 7

Answer

The print function has a default end of a linefeed. If you tell it to print a linefeed, it will do that, and then add the default ending linefeed, resulting in two linefeeds. If you only want one, call it with no arguments:

print()