• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/377685/20
CodePython

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.

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

  • 45+ Interesting Python Project Ideas
  • 60+ Interview Coding Questions

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!

Python Interview Questions eBook

commandPython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Dephiny
    May 27, 2019 at 10:00 pm

    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

    • Reply
      Aniruddha Chaudhari
      May 27, 2019 at 10:37 pm

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

      • Reply
        Sudhakaran
        August 27, 2019 at 11:10 pm

        Quotation marks inside the os.system() are wrong.

        Use ‘clear’ instead of ‘clear’.

        • Reply
          Aniruddha Chaudhari
          August 27, 2019 at 11:27 pm

          Hi Sudhakaran, Thanks for notifying.

          Looks like there is an issue with representation.

      • Reply
        Sulaiman
        January 17, 2021 at 3:40 pm

        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

        • Reply
          Aniruddha Chaudhari
          January 17, 2021 at 4:45 pm

          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.

  • Reply
    Dephiny
    May 28, 2019 at 1:10 am

    Hi Aniruddha,

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

    • Reply
      Aniruddha Chaudhari
      October 5, 2019 at 9:18 am

      Dephiny, this is really strange. I will let you know if I find any solution.

  • Reply
    myusrn
    October 4, 2019 at 9:52 pm

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

    • Reply
      Aniruddha Chaudhari
      October 5, 2019 at 9:25 am

      Assign the return value to val. “0” will not get printed on console.

      import os
      clear = lambda: os.system('clear')
      val=clear()
      
      • Reply
        Anton Gruba
        December 28, 2019 at 10:16 pm

        use:

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

        …as the commentator below mentioned though for a different reason

  • Reply
    Justin
    October 24, 2019 at 9:20 am

    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
  • Reply
    thekinglywe
    November 3, 2019 at 5:16 am

    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!

    • Reply
      Aniruddha Chaudhari
      November 4, 2019 at 4:18 pm

      Great. Lambda is an advanced Python concept. It comes very handy while working on real projects.

  • Reply
    Martin Bergeron
    January 16, 2020 at 8:14 am

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

    • Reply
      Aniruddha Chaudhari
      January 19, 2020 at 9:28 am

      You’re welcome, Martin. I believe, giving realtime use cases makes learning easy and more clear.

  • Reply
    Joaquin Mancilla
    April 1, 2020 at 10:33 pm

    I finally understood what for lambda is for thanks to your example. Thanks for posting.

    • Reply
      Aniruddha Chaudhari
      April 1, 2020 at 11:39 pm

      You’re welcome, Joaquin!

      If you are interested in learning Python, you can join our Python learning group.

  • Reply
    XueQing
    April 27, 2021 at 6:03 pm

    It’s really a simple and clear solution.

    • Reply
      Aniruddha Chaudhari
      April 28, 2021 at 4:12 pm

      Glad you find it simple and clear.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard