You can always bet on Python. Its applications are on numerous platforms. Being a general-purpose programming language Python can prove vital in web development, Data Science, Automation, and much more. You can find various modules in python which helps to automate or simplify many tasks. Today we will be looking at how to clear screen in python for windows and Linux systems. 

If you use editors then you can interrupt the code whenever you want. The need to clear screen in python usually arises if you are using a terminal or shell for coding. If you are struct with messy code and complexations and want to erase the screen, you can do it with the help few lines of python code. 

Let’s see how it works.

How to Clear terminal in Windows

We are going to start the clear process in the terminal in windows. For this, you have to type anything on the command terminal and later you have to use the following code to erase or clear the entire terminal.

import os
os.system("cls")

We are importing the OS module to access the operating system. Then, we have used the “cls” command to clear screen the entire terminal.

  • You can see the illustration here. I have created some random text and then imported the OS module.
  • Then, I have used the “cls” command to clear the entire terminal, which you can see in the below image.

Wow! That’s it. We have cleared the terminal with ease.

Clear screen in python using functions

Now, in this section, we are going to –

  • Import OS module.
  • We will define a system call with “clear” and “cls” commands for both Linux and windows os.
  • Then, we can call the function we defined.
#import os module
from os import system, name 
from time import sleep 
#Define function   
def clear(): 
    #For windows
    if name == 'nt': 
        _ = system('cls') 
    #For Linux
    else: 
        _ = system('clear') 
print('Welcome to Hackanons\n'*10)  
sleep(2) 
 #Clear screen 
clear() 
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons
Welcome to Hackanons

The above code will print the “Welcome to Hackanons” statement 10 times and it will sleep for 2 seconds. After that, the terminal will be cleared.

Clear screen in Python console

Similarly, we have another piece of code to clear screen in python console. Let’s see how it works.

For Windows
 import os
clear = lambda: os.system('cls')
clear()

For Linux 
clear = lambda: os.system('clear')

The above code will do things for you. Note that, if you import the OS module with the system, then you don’t need to mention os.system in the code. If you don’t import the system module, then use the code as above i.e. os.systsem.

You can also see the different codes for windows and Linux systems. With a slight change, you can use the python code to clear the python console. 

Clear python interpreter screen

To clear the python interpreter screen, you have to run the following code. Don’t forget to import the OS module :).

import os
# For Windows system
os.system('cls')
# For Linux system 
os.system('clear')

Wrapping Up

There will be times, you need to clear your terminal screen, console, or interpreter screen as well. You can do this using ctrl + L as well if you use any editors. This article covers most of the aspects and operating systems of the clear screen in python. All you need to do is to see the above code and copy or type this and run. Everything will be cleared on your terminal, console, and interpreter. That’s all for now. Happy python!!!.

Categorized in: