[FIXED] Carriage return Not Working with Print in VS Code

5/5 - (1 vote)

FIX: To fix the carriage return now working issue in your IDE, you must directly use the terminal to execute the code instead of using the built-in output console provided by the IDE.

Problem Formulation

It is a common issue in many IDEs, including VS Code and PyCharm, wherein the carriage return character (‘\r’) does not work properly within the print statement.

Example: Consider the following code where we are attempting to overwrite the previous print to the same line:

import time
li = ['start', 'Processing result']
for i in range(len(li)):
    print(li[i], end='\r')
print(end='\x1b[2K') # ANSI sequence to clear the line where the cursor is located

Expected Output:

Actual Output: Unfortunately, when we execute this code in VS Code and run it in the OUTPUT console, this is how the output looks:

πŸ›‘ Thus, the actual output defeats the purpose of the code as the print statement displays the strings in new lines, which is exactly what we want to avoid.

Reason: The question here is – “Is the code wrong?” Well, there is no issue with our code. Let’s get to the root of the problem.

The OUTPUT console in VS Code exhibits a slightly different behavior than the standard output terminal. Some GUI-based IDEs don’t work properly for the Carriage return character (“\r“).  Hence, even though the code is correct, the output console of the IDE is not working properly for the carriage return within the print statement.

πŸ“ŒHighly Recommended Read: How to Overwrite the Previous Print to Stdout in Python?


[FIXED] Carriage return Not Working with Print in VS Code

The straightforward solution to this problem is to run the code in the standard output terminal instead of executing the code in the output console.

Note: If you are facing problems with buffering the output you can use the flush='True' parameter within the print statement as shown below.

import time
li = ['start', 'Processing result']
for i in range(len(li)):
    print(li[i], end='\r', flush=True)

Let’s dive into the different ways to execute the code in the terminal to get the desired output:

Method 1

  • Select Terminal
  • Select the PATH, which contains the .py script. In my case, it is: D:\VS Code Python Scripts. So this is the command to navigate to this path (in WINDOWS): cd 'D:\VS Code Python Scripts'
    • Note that I have used ' ' to enclose the path to avoid any command-line error because of the spacing in the filename.
  • Once you are at the specified Path, use the normal Python command to run your script: python same_line_print.py


Method 2

  • Right-click on the code
  • Select Run Python file Terminal

Method 3

If you are using the Code Runner Extension to run your code in VS Code:

  • Click on the  down arrow button just beside the Run button. A drop down menu appears.
  • Select Run Python File (Don’t select Run Code)

Solving the Issue in PyCharm

The same issue can be observed in the PyCharm IDE as well. The solution in this case is quite similar, i.e., run the code directly in the terminal.

  • Select Terminal
  • Type the normal Python command to execute the program:
    • python 'carriage return.py'


Thus, the bottom line is – Though the code is correct, it is the console of the IDE that misbehaves and obstructs the carriage return, which denies us the kind of output we want. Hence, the simple solution to this is to use the terminal to run your code from within the IDE.

Related Read: Best Python IDE and Code Editors [Ultimate Guide]

I hope this tutorial helped you. Please subscribe and stay tuned for more solutions and tutorials. Happy learning! πŸ™‚

To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members: