Carriage return VS Tab - My super doubt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Carriage return VS Tab - My super doubt

Could someone please explain me what is the DIFFERENCE between CARRIAGE RETURN " \r" and TAB "\t" ?? If we run them in code playground, they show the same outputs 🤔

12th Mar 2022, 1:53 PM
Aurora Borealis
Aurora Borealis - avatar
8 Answers
+ 2
So let's say I have this code: print('Python is noice') #Output: Python is noice print('Python is noice\r123456') #Output: 123456 is noice The text at the start of the string is replaced by the text after the carriage return ;)
12th Mar 2022, 2:04 PM
HelloWorld
+ 2
Gursimran Kaur where did you run the code? \r does not seem to work in the sololearn code playground. I recommend you try using pydroid3 or some other IDE, because my code works exactly as I wrote above there.
12th Mar 2022, 2:51 PM
HelloWorld
+ 1
Note: \r will not work in all teminals/environments! But when it works: >>> print(“abcdef\t123”) abcdef 123 >>> print(“abcdef\r123”) 123def
12th Mar 2022, 3:47 PM
Per Bratthammar
Per Bratthammar - avatar
0
HelloWorld But your program gives the following output - Python is noice Python is noice 123456 😑
12th Mar 2022, 2:19 PM
Aurora Borealis
Aurora Borealis - avatar
0
HelloWorld Oh! So that's it !! I knew \t but when it and \r gave same outputs I thought that I'm wrong. It means I've learnt wrong from the lesson as when I tried the code given there, it didn't replace text after carriage return. I thought they work same....
12th Mar 2022, 3:44 PM
Aurora Borealis
Aurora Borealis - avatar
0
That was indeed one weird implementation of carriage return.
12th Mar 2022, 4:05 PM
Ipang
0
Ipang Yes.
12th Mar 2022, 4:24 PM
Aurora Borealis
Aurora Borealis - avatar
0
Gursimran Kaur, I might be mistaken, but in real typewriters (keyboards ancestor), a carriage return is used to roll the paper up one line and prepare to write more text back at the left margin. If \r means to replace text in the beginning, then Python had just betrayed the original use case of carriage return. I hope these links provides a bit of info https://en.m.wikipedia.org/wiki/ANSI_escape_code https://www.log2base2.com/programming-language/python3/basic/escape-sequence-in-python.html
12th Mar 2022, 4:41 PM
Ipang