+ 3

Usage of Carriage Return (`\r')

The question about the difference between the new line (`\n') and the carriage return (`\r') escape characters, probably already has been asked, and I know that the new line escape character mainly just does what it says (I need it quiet often), and the carriage return - brings you back to the beginning of the same line - OK, ...so far. I'd like to ask fellow coders, what they use '\r' for and how it can be helpful it which kind of situations. Thank you for your time.

23rd May 2024, 2:44 AM
Konan
Konan - avatar
9 Antworten
+ 5
The word "carriage" itself comes from the age of mechanical typewriters, where you insert the paper into a contraption that moves one character to the left every time when you hit a key. And the paper is rolled by one line when you hit the new line. So in an ancient typewriter these are two different functionalities. It is rather annoying how different OS' treat a new line control character in IT, leading to incompatibility issues of even simple text files. One interesting use case for just the \r would be a progress indicator in a console application. For example when you run a long process such as downloading a large file, and want to print the status on the screen occasionally, to inform the user. Just reset the position of the cursor to the beginning of the line and print the readiness % over the old number, rather than adding a new line.
23rd May 2024, 5:28 AM
Tibor Santa
Tibor Santa - avatar
+ 5
Konan If Sololearn playground version would allow the carriage return ( \r ) it would operate as such from __future__ import print_function print('hello world\r123') # display as 123lo world print('hello world\r1234299') # display as 1234299orld https://www.sololearn.com/Discuss/3046458/?ref=app
23rd May 2024, 3:27 AM
BroFar
BroFar - avatar
+ 4
BroFar , the use of importing: from __future__ import print_function is only required in a python 2 environment to make some behaviors like print compatible with python 3.
23rd May 2024, 2:51 PM
Lothar
Lothar - avatar
+ 3
Thank you for this example. This seemed to has been keeping a lot of programmers busy over the time - here is an interesting thread I found on stackoverflow about the topic (definitely worth to take a little peek): https://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n
23rd May 2024, 4:27 AM
Konan
Konan - avatar
+ 3
Bob_Li That's odd. It wasn't working in PoweShell, and now it does. Must be something I overlooked. However, it doesn't work in IDLE.
23rd May 2024, 12:36 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
It inspires me to ask a related question instead of asking in a new post, since they are related. Say I want to emulate a terminal, outputting "hello" by each character, and then delete each character. And this is the code: from time import sleep def main(): txt = 'hello' for i in txt: print(i, end='', flush=True) sleep(0.1) for j in range(len(txt)): # print('\b \b', end='', flush=True) # I think it works the same as below? print('\b', end='', flush=True) # back one space print(' ', end='', flush=True) # overwrite the character print('\b', end='', flush=True # back one space again for next iteration sleep(0.1) main() I'm expecting the screen will show as below after "hello" is printed, all in the same line, but it doesn't either in IDEL or PowerShell. hell hel he h Am I misunderstanding the backspace \b usage?
23rd May 2024, 11:05 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming your code works in Pydroid. Also in Powershell7
23rd May 2024, 12:18 PM
Bob_Li
Bob_Li - avatar
0
😁you should try colors and emojis in different consoles.
23rd May 2024, 1:02 PM
Bob_Li
Bob_Li - avatar
0
Salut
24th May 2024, 11:01 PM
Saa souro Leno
Saa souro Leno - avatar