Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print spaces between values in loop?

How to put spaces between values in a print() statement for example:

for i in range(5):
    print(i, sep='', end='')

prints

012345

I would like it to print

0 1 2 3 4 5
like image 352
user1707093 Avatar asked Feb 20 '23 00:02

user1707093


1 Answers

While others have given an answer, a good option here is to avoid using a loop and multiple print statements at all, and simply use the * operator to unpack your iterable into the arguments for print:

>>> print(*range(5))
0 1 2 3 4

As print() adds spaces between arguments automatically, this makes for a really concise and readable way to do this, without a loop.

like image 128
Gareth Latty Avatar answered Feb 27 '23 10:02

Gareth Latty