+4 votes
in Programming Languages by (73.8k 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

+1 vote
by (348k points)
selected by
 
Best answer

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


...