0

n = 10 while n > 0: print(n) n=n-1

so far i have this which gives me

10
9
8
7
6
5
4
3
2
1

but I want
10 9 8 7 6 5 4 3 2 1

  • 2
    Possible duplicate of [Print in one line dynamically](https://stackoverflow.com/questions/3249524/print-in-one-line-dynamically) – Vincent Oct 07 '19 at 23:28

2 Answers2

0

You should use the 'end' parameter of print. By default, end="\n" in print, a newline character

n = 10
while n > 0:
    print(n, end=" ")
    n=n-1
san
  • 1,415
  • 8
  • 13
0

There are several ways you could do that.

Firstly, and probably the simplest, you can use the end argument of print().

For instance, you could do something like this:

n = 10
while n > 0:
    print(n, end=" ") # note the 'end' argument
    n=n-1

Which gives : 10 9 8 7 6 5 4 3 2 1 (with a trailing space and without carriage return).

Another way I can think of, but involves a bit more complexity in my opinion, is using join().

Example:

print(" ".join(str(i) for i in range(10, 0, -1)))

Which gives : 10 9 8 7 6 5 4 3 2 1 (with a carriage return and without trailing space).

ImranD
  • 320
  • 1
  • 6