Hussein Ibrahim
Hussein Ibrahim

Reputation: 21

Carriage return does not work with me correctly

Carriage return or \r works as you have shifted your cursor to the beginning of the string or line. so, whenever you will use this special escape character \r, the rest of the content after the \r will come at the front of your line and will keep replacing your characters one by one until it takes all the contents left after the \r in that string. right???

However, with me it only prints what is after \r so,

print("don't wish for it work \r for it")

print only "for it"

Upvotes: 0

Views: 744

Answers (3)

AnkurSaxena
AnkurSaxena

Reputation: 825

Other answers seem to be providing little explanation for what's the problem. When you are running your code, two cases can happen:

  1. \r starts to overwrite character from previous line (without clearing the entire line) which will give you following output:
print("don't wish for it work \r for it") 
 |don't wish for it work    -- line 1
+| for it                   -- line 2
 | for itish for it work    -- line 1 overwritten by line 2 (only overlapping chars)
  1. \r clears the previous line and writes content following \r, which will give you following output:
print("don't wish for it work \r for it") 
 |don't wish for it work    -- line 1
+| for it                   -- line 2
 | for it                   -- line 1 cleared. line 2 is output

Since behaviour of carriage return can vary from system to system, consider using \n as shown in @KJDII's answer or understand and configure the behaviour for which system you will run it on.

Upvotes: 0

PGS
PGS

Reputation: 79

If you run the print statment from terminal, the output will be

 for itish for it work 

If you run the exact statement from IDE (Pycharm), the output will be for it. This could be due to a bug/feature. In order to get the exact output in Pycharm IDE, go to Run > Edit Configuration > Emulate Terminal in output console and select this option. You will get the exact result as described in your statement.

 for itish for it work 
 

Upvotes: 0

KJDII
KJDII

Reputation: 861

Try:


print("don't wish for it work \n for it")

Upvotes: 0

Related Questions