How to clear the interpreter console?

How to clear the interpreter console?

Hello Prynka,

As you mentioned, you can use a system call to clear the console:

For Windows:

import os
clear = lambda: os.system('cls')
clear()

For Linux:

import os
clear = lambda: os.system('clear')
clear()

Hey Prynka,

Here’s a more cross-platform way to clear the screen using a function:

import os

def cls():
    os.system('cls' if os.name == 'nt' else 'clear')

# Now, to clear the screen
cls()

Combining this with the lambda suggestion above can help save a line of code. Handy as hell! Thank you! :slight_smile:

Hey Prynka,

Here’s a way to create a cross-compatible cls function in Python:

from __future__ import print_function

# Define cls for clearing the screen
cls = lambda: print("\033c", end='')

# Now, to clear the screen
cls()

You can call cls() from the terminal to clear the screen.

Please note that \033c clears everything on the screen, including the scrollback buffer. This behavior may differ from \033[H\033[J, which only clears the visible screen and may not clear the scrollback buffer.