How to Clear Screen in Python?
Python os module is imported to clear the console screen in any operating system. The system() method of the os module with the string cls or clear as a parameter is used to clear the screen in windows and macOS/Linux, respectively.
- This article will teach us how to clear the console screen in different operating systems.
- We will use the os module and its system() method.
- We will learn about clear and cls commands and how to use them as a string parameter to the system() method.
Introduction to Python Clear Screen
Suppose there is a case where you have given the information of 3 students, and you have to print their details in a way like first there will be information of student 1 , then after some time information of student 2 will be displayed and then finally the information of student 3 will be displayed. So, in that case, we will have to clear the python console each time after displaying information about each student.
Image Explanation of the above Example
In an interactive shell/terminal , to clear the python console, we can use the ctrl+l command, but in most cases, we have to clear the screen while running the python script, so we have to do it programmatically.
We can clear screen programmatically, and it helps us to format the output in the way we want. We can also clear the output whenever we want many numbers of times.
How to clear the python console screen?
Clearing the console in python has different methods for different Operating Systems. These are stated below:
- In Windows: For clearing the console in the windows operating system, we will use the system() function from the os module with the 'cls' parameter.
Syntax of the system() function: system() function is in the os module, so the os module needs to be imported before using the system() function in Windows. After importing the os module, the string parameter 'cls' is passed inside the system function to clear the screen.
- In Linux and MacOS: For clearing the console in Linux and Mac operating systems, we will use the system() function from the os module with the 'clear' parameter.
Syntax: os module needs to be imported before using the system() function in Linux. After importing the os module string value parameter 'clear' is passed inside the system function to clear the screen.
- Ctrl+l: This method only works for Linux operating system. In an interactive shell/terminal, we can simply use ctrl+l to clear the screen.
Example of Python Clear Screen
Example 1: Clearing Screen in Windows Operating System
Let's look at an example of the clear screen in python to clarify our understanding.
We will use the above-stated system() function for clearing the python console.
First, we will print some Output; then we will wait for 4 seconds using the sleep() function to halt the program for 4 seconds, and after that, we will apply os.system('cls') to clear the screen.
The output window will print the given text first, then the program will sleep for 4 seconds, then the screen will be cleared, and program execution will be stopped.
Example 2: Clearing the screen, then printing some more information in Windows Operating System.
First we will print some Output, then we will wait for 1 second using the sleep() function, and after that, we will apply os.system('cls') to clear the screen.
After that, we will print some more information.
The output window will print the first information, then the program will sleep for 1 second, and the screen will be cleared, and then it will print the second information. Again the program will sleep for 1 second, and at last, the program will be stopped after printing the final batch of information.
Example 3: Clearing Screen in Linux Operating System
We will use the above-stated system() method for clearing the python console.
First, we will print some Output; then we will wait for 5 seconds using the sleep() function, and after that, we will apply os.system('clear') to clear the screen.
- After 5 seconds, the screen is cleared.
The output window will print the given text first, then the program will sleep for 5 seconds, then the screen will be cleared, and program execution will be stopped.
Example 4: What if we don't know what OS we are working on?
There can be a case where we must first determine what OS we are working on. So, we will first determine whether the os is Windows, Linux, or Mac.
For Windows, the os name is "nt" and for Linux or mac, the OS name is "posix".
So we will check the os name and then accordingly apply the function.
For this case, the os name is nt , so windows console will be cleared after 2 seconds of program sleep, and then it will be terminated.
Now that we have seen various examples of how to clear the screen in python let us note down a few points.
Clear Screen in Python
While working on the Python terminal (not a console)/interactive shell, some errors occur in the output. So, we have to clean the screen by pressing Control (Ctrl) and L keys simultaneously.
However, sometimes there are situations when the output has to be formatted properly to eliminate issues.
That’s why clearing the screen is based on the amount of output. In this situation, some commands need to be inserted in Python’s script to clear the screen as and when required.
So in this article, we will consider every possible way to clear the screen in Python without having any trouble or errors.
How to Clear Screen in Python Terminal
If you are working on a terminal, then use commands «cls» and «clear» to clear the terminal like this:
For Windows Users
If you are a Windows user and want to clear the screen in Python, you can easily do it using the «cls» command.
This command will instantly clear the screen, as you can see in the below image:
For Linux Users
Linux also has a command to clear screen in Python easily, so use the following command to do it:
Remember there is no specific function, built-in keywords, etc., available to clear the screen. That’s why users need to clear the screen according to them.
Clear Screen in Python Using Functions
Now let’s consider the functions we can use for clearing the screen in Python and remember that you can use these functions in both Windows and Linux.
Using click library
In this method, you can use click library to create a function, and it can work on both Windows and Linux.
In the above example, we are using click.clear() function from click library to clear screen.
This function works in Windows, Linux, and Mac operating systems.
Using \n (Newline character)
The whole way of not clearing the screen is as different as the answer is different. To remove any further error of the line, the user does not need to run the external process completely so that the screen can be cleared.
To clear the screen, you have to print new numbers which are higher than the height and can print something like this («\ n» * 10)
In the above example, we need to clear a few lined of the screen so we use “\n” with print, this will add a newline character and clear x number of lines.
Clear screen program by using os.system method
There are many OS platforms like Windows, Linux, and macOS, so we need different commands to clear the screen. So as you can see in the below syntax, we have used the ‘_’ variable for holding the value of the last expression in the interpreter.
Now we will describe the os.system method to clear the screen in Python without having any troubles. As you can see in the below syntax, we have used the os.system command because It is a string that tells which command needs to be executed.
We got «Screen Cleared» as we have printed it using the above syntax in the output.
In the above example first, we are checking if the operating system is Linux or not by checking the value of os.name method.
If the Operating system is Linux or Mac the value of os.name will be ”posix” .
And if the value of os.name is posix then we will run the function os.system(‘clear’) or else we will run function os.system(‘cls’) .
In the above example first, we check if the platform is Windows or Linux by using platform.system() function.
If the platform is “Windows” then we store the value of a command in the variable “cmdtorun” .
And at the end of the function, we are run the function os.system(cmdtorun) with the variable “cmdtorun” .
Using Subprocess Library
In this example, we have used the subprocess() function to execute an action for clearing the screen in output.
So this is how you can easily clear the screen in Python using functions or commands in the Syntax. As we have mentioned earlier,
There are many methods to clear the screen in Python without getting any errors. But, in our opinion, a clear screen using a click library is the easiest because it works with both the operating systems Unix, Windows, and macOS. And it doesn’t need checking the OS.
How to Clear Python Shell in the Most Effective Way
We all must have experienced one common thing while working in any programming language which is messy code and output. It rarely happens that we get the desired output in one go, so there are many hits and trials before that. But it is difficult to work on messy code and outputs so, we need a clear screen. If you have ever worked on c++ the function that helps us to achieve this goal is clrscr().
If we are working on an interactive shell, we can quickly clear the output by simply pressing ‘Ctrl + L.’ But when we are working on IDLE, command prompt, Linux Terminal in a running shell, we need to make our own functions to do so. So, let’s learn how to clear a python shell.
How to Clear Python Shell?
If we are working in a REPL terminal, to clear the output, we can make a function using the os module. An os module helps us to interact with our systems. A REPL terminal also known as read–eval–print loop terminal can be windows command prompt, your Linux terminal, or anaconda prompt. So, let us learn how to make a function to clear the python shell.
How to Clear Python Shell in Windows Command Prompt or Anaconda Prompt?
To clear screen in these terminals, we have first to import the os module using the code – import os.
Note- I am using RedHat version-8. The procedure of starting a python shell inside your terminal may vary from mine.
How to clear Shell in IDLE?
IDLE or (Integrated Development and Learning Environment) is an interactive shell where we can code or write programs in python. There is no such built-in function or way to clear the python shell, but we have a trick by which we achieve our goal.
We cannot use the functions that we used to clear the screen in the windows command prompt in IDLE.
Let’s see how we can do this –
In the video above, we can clearly see that our function is just moving the prompt 47 lines down and if we scroll up, we can see the previously written code.
We have learned how to clear python shell in the most effective way and most of the operating systems. We have also learned the trick to clear the screen when we do not have any built-in function for it.
4 Different Ways to Clear Console in Python
You must import the os module and use its os.system() method to clear the console programmatically in Python. The os is a built-in library that comes with Python3 installations.
I am using Mac, so I need to write Linux-related commands.
If you run the above command in your console, it will clear the screen.
For the Windows user, use the following code.
You can use the lambda function if you don’t want to use the full function.
And it will clear the console.
That’s pretty much it for clearing the console in Python.
Method 2: Using the subprocess.call() function
You can use the subprocess.call() function in the subprocess module that allows you to spawn new processes, connect to their input/output/error pipes and clear the console in Python.
If you run the above code, it will clear the console.
Method 3: Using the ANSI escape codes
ANSI escape codes are sequences of characters that can be included in a string to specify various types of formatting for terminal output, such as changing the color or cursor position or clearing the console.
It will clear the Python console.
The “\033” is the escape character, and c is the code to reset the terminal.
The “end” argument is set to an empty string to ensure no extra line break is added after the escape code.
Method 4: Using the ipykernel package in Jupyter notebooks
In Jupyter notebooks, you can clear the output of a cell by using the ipykernel package.
The ipykernel package provides an API for controlling the Jupyter kernel, including the ability to clear the output of a cell, or in our case, it is console programmatically.
The clear_output() function from the IPython.display module can be used to clear the output of a cell in a Jupyter notebook.
In Python, you can clear the console by using the following methods based on the platform you are using: