I found strange thing while studying through idle

Chris Angelico rosuav at gmail.com
Fri Mar 9 13:18:11 EST 2018


On Sat, Mar 10, 2018 at 5:10 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 9 March 2018 at 17:46, Rob Gaddi <rgaddi at highlandtechnology.invalid> wrote:
>> On 03/08/2018 07:57 PM, Steven D'Aprano wrote:
>>>
>>> [snip]
>>>
>>> But it is possible that due to differences between platforms, the
>>> OP's version of IDLE doesn't display a carriage return as \r but
>>> rather as an invisible zero-width space.
>>>
>>
>> Just to derail this conversation a bit, does anyone have a use case in the
>> modern (Py3) age for '\r'?  I use b'\r' fairly regularly when talking to
>> serial port devices.  But the string version?
>
> It's fairly common for backing up and rewriting a progress line in
> simple console programs:
>
> for i in range(100):
>     print(f"\r              \rCompleted: {i}%", end='')
>     sleep(0.5)
>

Yep, though I prefer end="\r" (or end="\33[K\r" if you can use ANSI
codes) rather than putting the \r at the beginning. It looks nicer to
have the cursor at the end of the line (which your way does, but mine
doesn't), but on the other hand, leaving the cursor at the beginning
means that any output lines (ending with a normal \n) will more
cleanly overwrite the completion percentage line. It's tradeoffs all
around, as always.

But yes, that's one VERY common use of \r.

ChrisA



More information about the Python-list mailing list