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 🤔
8 odpowiedzi
+ 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 ;)
+ 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.
+ 1
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....
+ 1
Note: \r will not work in all teminals/environments! But when it works:
>>> print(“abcdef\t123”)
abcdef 123
>>> print(“abcdef\r123”)
123def
0
HelloWorld But your program gives the following output -
Python is noice
Python is noice 123456
😑
0
That was indeed one weird implementation of carriage return.
0
Ipang Yes.
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