Do you use the Python interpreter console for practicing Python code?
You might have seen, that there is no direct way or command to clear Python interpreter console.
No worries. There is an 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:
cls
clear the console. clear
command works.We can run these system calls through Python to clear the shell 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 built 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.
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()
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 been implemented.
The above code is all about the lambda function.
The “lambda” keyword in Python is used to define anonymous functions. These functions are also called 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 the latest Python version. It will work for both Python 2.x
and 3.x
versions.
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 the Python shell interpreter console open. Sometimes test command runs over it and creates cluttered print output on the Python console. These backlog print command needs to be cleared. So use this trick to clear the screen in the Python shell.
Related Articles:
Hope you find these quick commands and guides useful for your Python learning. Is there anything I can help you with? Feel free to write in the comment section below.
Happy Pythoning!
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
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”.
Quotation marks inside the os.system() are wrong.
Use ‘clear’ instead of ‘clear’.
Hi Sudhakaran, Thanks for notifying.
Looks like there is an issue with representation.
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
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.
Hi Aniruddha,
Thank you for the reply, I am using Ubuntu 16.04.6 LTS (Xenial Xerus).
Tried your suggestion no luck.
Dephiny, this is really strange. I will let you know if I find any solution.
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
>>>
Assign the return value to val. “0” will not get printed on console.
use:
…as the commentator below mentioned though for a different reason
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:
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!
Great. Lambda is an advanced Python concept. It comes very handy while working on real projects.
Thanks for using
os.system('clear')
to introduce lambda functions. This is exactly how a simple answer can open new doors.You’re welcome, Martin. I believe, giving realtime use cases makes learning easy and more clear.
I finally understood what for lambda is for thanks to your example. Thanks for posting.
You’re welcome, Joaquin!
If you are interested in learning Python, you can join our Python learning group.
It’s really a simple and clear solution.
Glad you find it simple and clear.