print function and unwanted trailing space

candide candide at free.invalid
Sat Aug 31 11:58:54 EDT 2013


Le 31/08/2013 15:59, Peter Otten a écrit :


>
> To make it crystal clear, the above was to illustrate the algorithm used in
> Python 2, not a suggestion.


Ok sorry, I misinterpreted.




 > I still think you should live with a trailing space


Are you sure ? The following code

#----------------------------------
import io

output = io.StringIO()
n=5
for i in range(n-1):
     print(i, end=' ', file=output)
print(n-1, file=output)
print(output.getvalue().count(' '))
#----------------------------------


outputs 4, the correct number of space character.



 > or go with my actual
 > suggestion
 >
 > print(*range(5))
 >


It's a very good suggestion (the best one in fact) but rather 
complicated to explain to pure novices in programming.




More information about the Python-list mailing list