[Simple Steps] How to Clear Python Shell Interpreter Console Screen?

[Simple Steps] How to Clear Python Shell Interpreter Console Screen?

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:

  • For the window system, cls clear the console.
  • For the Linux system, 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.

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 been implemented.

What is Lambda Function here?

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.

Clear Python Interpreter Console

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 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!

20 Comments

  1. 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

      1. 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

        1. 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.

  2. Hi Aniruddha,

    Thank you for the reply, I am using Ubuntu 16.04.6 LTS (Xenial Xerus).
    Tried your suggestion no luck.

  3. 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
    >>>

  4. 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:

    import os
    def clear (): os.system('clear')
    

    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:

    def f(x): return 2*x

    No:

    f = lambda x: 2*x
  5. 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!

  6. Thanks for using os.system('clear') to introduce lambda functions. This is exactly how a simple answer can open new doors.

Leave a Reply

Your email address will not be published. Required fields are marked *