How to Clear Python Interpreter Console?
There is no direct way or command to clear Python interpreter console. We might have heard about system call to clear console screen. For window system, ‘cls’ clears the console. ‘clear’ command works for Linux system. We can use these system calls t0 clear screen in python.
To call system call on python console, it requires os library to be imported. This os library comes inbuilt with python installation. There is no need to explicitly install.
Command to clear Python Interpreter Console on Window:
import os clear = lambda: os.system('cls') #on Windows System clear()
Command to clear Python Interpreter Console on Linux:
import os clear = lambda: os.system('clear') #on Linux System clear()
Don’t Just Copy Paste the Code:
Why am I saying that?
If you copy paste the code, I am pretty sure you will forget this code and then tomorrow again you will do a Google Search.
The best way is to understand the code written above. It is pretty simple to remember and write code if you know how it has implemented.
It is just lambda function. The “lambda” keyword in Python is used to define anonymous functions. And these functions are also called as lambda functions. Know about lambda function you can write many such codes with no more hustle. It is extremely useful.
These commands are tested on Python 3.4 version. It will work for both Python 2.x and 3.x versions.
You can find more Python tricks and command on CSEstack.
I am Python developer. I usually keep Python interpreter console opened. Sometimes test command runs over it and creates cluttered print output on python console. These backlog print command needs to be cleared. So use this trick to clear screen in Python.