How to clear the concole

In matlab, if you want to clear the command window to get a tidy startup, you just write ‘clc’ in your code.

What is the counterpart in python?

by the way, I am using vs code.

Is this for windows?
Are you using the new windows terminal app?

If so you can use ANSI escape sequence to clear the terminal.

print("\x1b[2J")

If you are not using terminal app I do not have the code off the top of head that will clear the cmd.exe terminal.

1 Like

VS Code has its own console implementation, and you clear it using the IDE’s own functionality, not any language-specific functionality. See:

Matlab is a bit of a special case among programming languages; it defines its own environment. Most programming languages don’t have anything like a “clear console” command, because they don’t know you are using a console; your program only sees a stdin data stream and a stdout data stream, and the terminal (a separate program) is (by default) responsible for translating keystrokes into input and rendering the output with whatever font within a window. So generally, when you write a command-line program, you will “clear the screen” either by making a call to operating system functionality directly (usually a fair bit of work), by using the operating system to start a “clear screen” program (typically named cls on Windows and clear on Linux, or by outputting control characters that the terminal interprets (for example via the ANSI standard) - that last one is what Barry showed you. But when “the console” isn’t a terminal window, the rules can change again.

If you want something more like the “Matlab experience” you might be interested in Jupyter.

1 Like

I kind of like:

print("\x1b[2J\033[H")

a bit more since it clears then moves the cursor to the top left of the terminal. :slight_smile:

1 Like