+4 votes
in Programming Languages by (18.7k points)

When I print the elements of a list, it puts each element in different rows as shown below.

>>> for x in [1,2,3,4,5]:

...     print(x)

... 

1

2

3

4

5

How can I print the elements in one row?

1 Answer

0 votes
by (32.8k points)

You can add the parameter "end" to the print statement. The value of "end" should be a space(" "). Then print will put all elements in a row with a space between them.

Here is an example:

>>> for x in [1,2,3,4,5]:
...     print(x, end=' ')
...
1 2 3 4 5

...