Remove space from the end of each line of output (Python)

Viewed 40
n = int(input("Size: "))
for i in range(n-1, 0, -1):
    for j in range(0, i+1):
        print(j, end=' ')
    print('\r')
print(0)

This code is totally okay but prints whitespace with end of each line in the output.

2 Answers

Maybe if you did it like this:

n = int(input("Size: "))
for i in range(n, 0, -1):
    print(*range(i))

Output:

Size: 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

There will be no trailing whitespace characters if you do this

This will work:

n =int(input("Size: "))
for i in range(n-1, 0, -1):
    for j in range(0, i+1):
        print(j, end=' ') if j<i else print(j, end='')
    print('\r')
print(0)
Related