[Simple Steps] How to Clear Python Interpreter Console Screen?
Do you use the Python interpreter console for practicing Python code?
You might have seen, there is no direct way or command to clear Python interpreter console.
No worries. There is indirect way of doing this. Let’s see.
You can clear the Python interpreter console screen using a system call.
System calls to clear the console:
- For the window system,
cls
clear the console. - For the Linux system,
clear
command works.
We can run these system calls through the Python to clear screen. It’s simple.
To call the system calls through the Python console, we need the os
library to be imported. This os
library comes inbuilt with python installation. There is no need to explicitly install it.
Watch this video where I have demonstrated how you can clear the Python shell interpreter console.
Command to clear Python Interpreter Console on Window
Using Python Definition:
import os
def clear(): os.system('cls') #on Windows System
clear()
If you know the basic Python syntax, this code is self explanatory.
Using Lambda Function:
import os
clear = lambda: os.system('cls') #on Windows System
clear()
Command to clear Python Interpreter Console on Linux:
Using Python Definition:
import os
def clear(): os.system('clear') #on Linux System
clear()
Using Lambda Function:
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.
What is Lambda Function here?
Above code is all about lambda function.
The “lambda” keyword in Python is used to define anonymous functions. And these functions are also called as lambda functions.
Learn more 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.

What’s Next?
You can run a Python program without using the Python interpreter console. Install free text editor for your system (Linux/Windows/Mac). And now you can run the Python program from Windows’s command prompt or Linux’s terminal.
I am a 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.
Related Articles:
Hope you find this quick commands and guide useful for your Python learning. Is there anything I can help you with? Feel free to write in the comment section below.
Happy Pythoning!
Comments
Dephiny
Hi Aniruddha,
I tried the above on how to clear python interpreter console screen but keep getting this error:-
>>>import os
>>>clear = lambda: os.system(‘clear’)
>>>clear()
TERM environment variable not set.
256
Any suggestions on how to fix this would be great. On the terminal it shows the TERM is already set, as seen below
$ echo $TERM
xterm-256color
$ set | grep TERM
TERM=xterm-256color
Many thanks
Aniruddha Chaudhari
Hi Dephiny,
What Linux distro you are using?
Looks like there is an issue with the TERM environment variable. Try to export TERM variable “export TERM=xterm-color”.
Sudhakaran
Quotation marks inside the os.system() are wrong.
Use ‘clear’ instead of ‘clear’.
Aniruddha Chaudhari
Hi Sudhakaran, Thanks for notifying.
Looks like there is an issue with representation.
Sulaiman
My question on udemy QA is how to clear output in other python IDE like pycharm because the lecture on udemy is taught using Jupyter Notebook ipython. clear was used I’m not referring to python interactive interpreter, please
Aniruddha Chaudhari
I’m not sure which Udemy QA you are referring to. But, most of the IDE including PyCharm uses the Python interpreter console internally to execute the program. In this case, the solution mentioned in this tutorial will work.
Dephiny
Hi Aniruddha,
Thank you for the reply, I am using Ubuntu 16.04.6 LTS (Xenial Xerus).
Tried your suggestion no luck.
Aniruddha Chaudhari
Dephiny, this is really strange. I will let you know if I find any solution.
myusrn
is there a way to keep the ‘0’ from showing up, i.e. instead of ‘clear()’ producing
0
>>>
can this be authored to produce just
>>>
Aniruddha Chaudhari
Assign the return value to val. “0” will not get printed on console.
Anton Gruba
use:
…as the commentator below mentioned though for a different reason
Justin
ctrl + l
(That’s a lowercase L) clears the prompt too, on Linux. Also, the 0 is returned because of using lambda. If you insist upon using this method instead of ctrl + l, you can just do a function def instead:lambda functions should be exclusively used as callback functions, in functions that return functions, and as throwaway functions such as its use with the map. Don’t abuse it elsewhere.
From the PEP8 Python style guide:
Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.
Yes:
No:
thekinglywe
Thanks for the article! Even though I ended up going with one of the suggestions in the comments to clear the screen(ctrl l) I learned about a new topic i should probably look into (lambda). I appreciate the content!
Aniruddha Chaudhari
Great. Lambda is an advanced Python concept. It comes very handy while working on real projects.
Martin Bergeron
Thanks for using
os.system('clear')
to introduce lambda functions. This is exactly how a simple answer can open new doors.Aniruddha Chaudhari
You’re welcome, Martin. I believe, giving realtime use cases makes learning easy and more clear.
Joaquin Mancilla
I finally understood what for lambda is for thanks to your example. Thanks for posting.
Aniruddha Chaudhari
You’re welcome, Joaquin!
If you are interested in learning Python, you can join our Python learning group.
XueQing
It’s really a simple and clear solution.
Aniruddha Chaudhari
Glad you find it simple and clear.