[Tutor] Printing without a line feed

dn PyTutor at DancesWithMice.info
Thu Mar 25 02:35:33 EDT 2021



On 25/03/2021 18.29, Phil wrote:
> Thank you for reading this.
> 
> I want to print a range of numbers with a space between them but without
> a space at the end. For instance:
> 
>     for i in range(6):
>         print(i, end=' ')
> 
> How do I do this without the end space? I've spent hours on this even
> though I've managed to do what I want not long ago but I cannot remember
> how I did it. It's very frustrating.

end= and sep=
https://docs.python.org/3/library/functions.html#print

NB if there is an end=nullstring within the loop, the cursor remains at
the end of the 'printed' line.
Thus, afterwards, another print() will 'end' the line!

Alternately, use the loop to build a string or a list, which can then be
join-ed with space(s) - and print after the loop terminates.
-- 
Regards,
=dn


More information about the Tutor mailing list