Clear Console in Python

Do you know how to clear code using python? I am very stuck on it as am new

Hey @21Serjeante, welcome to the community!

You can use the Replit package:

from replit import clear

...

clear()
2 Likes

Welcome to Ask! The solution proposed by Qwerty works. To make it work when you’re not on Replit, use this:

clear = lambda: print("\033c", end="", flush=True)
clear()
2 Likes

hi there I will give an explanation. You can essentially print something called an ANSI code which when printed the computer instead of giving text interprets as something. For this case we can use one of the following:

# method 1, slow
import os
os.system('clear')


# method 2, faster
print("\033[2J\033[H", end="", flush=True) # clears, ensures no new line
                                           # and ensures immediate print
# the ansi code here is interpreted as clearing the screen with `\033[2J` but it
# doesn't move the cursor back to the beginning which is why `\033[H` is used 

# method 3 simplest
print("\033c", end="", flush=True) # see above for why there are other params included
# this has some benchmarks which say it is also the fastest

4 Likes

This is what replit.clear() does btw. It’s harder to remember so I just recommend that people use from replit import clear.

My solution does work when you’re not on Replit. pip install replit. There.

4 Likes

yeah I just was making sure I explained

3 Likes

If storage is a concern (replit has a lot of dependencies), my main package (firepup650) also contains the same clear that replit does. (With an optional boolean parameter to use os.system("clear||cls") instead)

3 Likes

But why? It’s so much slower

1 Like

Mainly just so the option is there, should someone want it.

2 Likes

A post was split to a new topic: How to make two buttons at the same time with tkinter

These are pretty much the fastest and most efficient ways to clear the console in Python. Any faster approach would likely require low-level programming or platform-specific optimizations, which would most likely add complexity to your code and probably wouldn’t be worth the effort.

3 Likes

yup from some benchmarks I found negligible but small diff of \033c being the fastest. I’m thinking optimizations would have to start from os level to get any faster

1 Like

also see:

1 Like

By the way, I have seen so many topics which’s answer is already present long time ago on another topic.

So if you want to save your own time and others’, you can click the search(magnifier icon) button on the top right of the page, and search for your problem(ie ‘python clear screen’ for this case). You may find the exact topic of your problem, and save your time of waiting for help.

Is it me or the tone is kinda rude? Idk but still sorry if this makes you unwell

5 Likes

Use one of the following (most reliable)

# First Method
import os
os.system('clear')

# Second Method
print('\033c', end = '', flush = True)

As previously mentioned already:

2 Likes