Ask coding questions

← Back to all posts
Is there a command to clear the screen in python?
MesyetiIsTaken (56)

Out of curiosity is there a command to clear the screen in python?

Answered by Bookie0 (6310) [earned 5 cycles]
View Answer
Comments
hotnewtop
Bookie0 (6310)

os.system(‘clear’)
But first import os
Also what I like to do is put that in a function that I call clear():

def clear():
   os.system(‘clear’)

So that it’s easier to call it

Kopamed (193)

@MesyetiIsTaken
Yes. This is the best one. The others take wayyyy to long to work so here it is:

import time
def cler():
    print("\x1b[2J\x1b[H",end="")

print("I will be gone soon")
time.sleep(2)
cler()
CodingCactus (4367)

Yes, just print("\033[2J\033[H", end="")

CodingCactus (4367)

you can put in function if you want:

def clear():
  print("\033[2J\033[H", end="")
CodingCactus (4367)

@MesyetiIsTaken no problem, it is nice because there are no needs for imports and also very few lines

Lord_Poseidon (170)

Specific to repl.it, use the package manager to install the replit package and call replit.clear() to clear the screen.

Mark as answer if this helps!

ApoorvAgrawal (51)

@Lord_Poseidon If I needed to clear the screen, I personaly wouldn't use replit.clear(), because if you want to download the code and run it on off your browser, it wouldn't work and you would have to replace it with either

import os
os.system('clear')

or

os.system('cls')

if you are on windows

Lord_Poseidon (170)

@ApoorvAgrawal still, it is platform specific. I get your point though.