print function and unwanted trailing space

Peter Otten __peter__ at web.de
Sat Aug 31 06:31:32 EDT 2013


candide wrote:

> Le 31/08/2013 10:43, Andreas Perstinger a écrit :
> 
>  > How about
>  >
>  >  >>> print(" ".join(str(i) for i in range(5)))
>  > 0 1 2 3 4
>  >
> 
> 
> Thanks for your answer. The output is stricly the same but the code
> doesn't suit my needs :
> 
> 1) I'm porting to Python 3 a Python 2 full beginner course : the
> learners are not aware of the join method nor the str type nor
> generators stuff;
> 2) Your code introduce a (sometimes) useless conversion to str (consider
> a string instead of range(5)).

You are out of luck, the softspace mechanism, roughly

softspace = False
for i in range(5):
    if softspace:
        print(end=" ")
    print(i, end="")
    softspace = True
print()

with `softspace` saved as a file attribute, is gone in Python3. But I don't 
think that someone who doesn't know it existed will miss the feature.

Maybe you can allow for /some/ magic and introduce your students to

print(*range(5))

early-on.




More information about the Python-list mailing list