print function and unwanted trailing space

candide candide at free.invalid
Sat Aug 31 09:33:35 EDT 2013


Le 31/08/2013 12:31, Peter Otten a écrit :
 > softspace = False
 > for i in range(5):
 >      if softspace:
 >          print(end=" ")
 >      print(i, end="")
 >      softspace = True
 > print()


The if instruction imposes useless testing (we know in advance the 
problem to occur at the very end of the loop) and useless writing 
(writing '').

The following is clearer

# -------------------------
n=5
for i in range(n-1):
     print(i, end=' ')
print(n-1)
# -------------------------


but doesn't solve all the cases (imagine a string or an iterator).



More information about the Python-list mailing list