How to Clear Console

I’m trying to figure out how to clear the Python console in Replit. I tried using ‘import clear’ but it didn’t work. Can someone help me?

Two ways:

from replit import clear

clear()
from os import system

system('clear')

Hey there, I wanted to add another solution

print("\033[2J\033[1;1H", end="", flush=True) is what replit.clear() does I believe ("\033[H\033[2J" doesn’t work I believe since it will clear the screen after moving the cursor to the beginning of the console which is what "\033[1;1H" is used for, and therefore the cursor will still be at the bottom).

While these all seem to be good options, os.system("clear") is MUCH slower than "\033[2J\033[1;1H" and "\033c", and "\033c" being slightly faster than "\033[2J\033[1;1H"

the following test was used:

from replit import clear
from ReplScreen import Screen
from time import time
from os import system

screen = Screen()

replit_start = time()
for i in range(1000):
    clear()
replit_end = time()

ReplScreen_start = time()
for i in range(999):
    print(screen.clear(True), flush=False)
screen.clear()
ReplScreen_end = time()

os_start = time()
for i in range(1000):
    system("clear")
os_end = time()

print("replit's clear took", replit_end-replit_start)
print("ReplScreen's clear took", ReplScreen_end - ReplScreen_start)
print("os's clear took", os_end-os_start)

image

1 Like

Press the X button on the top-right on the console

In replit use

from replit import clear
and
clear()

In any platform other than replit (but yeah ur can still use this code here, just more lines) you can use

from os import name as user, system as sys

def clear():
if user == ‘nt’:
_ = sys(‘cls’)
else:
_ = sys(‘clear’)

1 Like