Table of contents
1.
Introduction
2.
How to clear the Screen in Python? 
2.1.
Clearing Screen in Windows Operating System
2.1.1.
Using the cls command 
2.2.
Clearing Screen in Linux/Unix Operating System
2.2.1.
Using the clear command
2.3.
Clearing Screen when the Operating System is Unknown
2.3.1.
Method 1: Using the os.system() method 
2.3.2.
Method 2: Using the call method of the subprocess module
3.
How Shell of Python look like?
4.
What are the Commands to Work on Python Shell? 
4.1.
os.system()
4.1.1.
Example 1:
4.1.2.
Example 2:
4.2.
subprocess.Popen() 
4.2.1.
Example:
4.3.
subprocess.run()
4.3.1.
Example:
5.
What is the clear command for Python Shell?
6.
Frequently Asked Questions
6.1.
What is the shortcut for clear in Python?
6.2.
How do I clear only part of a screen in Python?
6.3.
How do you clear the screen in Python VS code?
7.
Conclusion
Last Updated: Mar 28, 2025
Medium

How to Clear Screen in Python?

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is the most widely used programming language in the world. If you are a Python developer or love to create various applications using Python, you end up working with Python interactive terminal/shell. Using the shell/terminal, you can write various commands to control your program. Several times you end up getting messy output in the terminal and want to clear the shell/terminal. 

In this blog, we will learn different ways to clear a command shell/terminal screen in Python while running a Python script.

How to clear the Screen in Python? 

In an interactive shell/terminal, we can simply use ctrl+l to clear the shell/terminal. There are several commands that can be used to clear screen in Python. Some of the commands are clear, cls, etc. 
But what if we want to clear a command shell/terminal screen in Python while running a Python script? To do so, we need to write our own code/program. We will discuss the methods for the three operating systems macOS, Windows, and Linux.

Also see, Python Filter Function

Clearing Screen in Windows Operating System

Using the cls command 

We can use cls command with the help of os.system() method to clear python terminal/shell in the windows Operating System.

Let’s understand how to clear screen in python using the cls command. 

'''Importing os and time module'''
import os
import time


'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)


'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)


'''Clearing the Screen after 5 second halt '''
os.system('cls')
You can also try this code with Online Python Compiler
Run Code


Output:

Initially-

Initially Output

After 5 seconds-

Output after 5 seconds


Clearing Screen in Linux/Unix Operating System

Using the clear command

We can use clear command with the help of os.system() method to clear python terminal/shell in the Linux/Unix operating system.

Let’s understand how to clear screen in python using the clear command. 

'''Importing os and time module'''
import os
import time


'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)


'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)


'''Clearing the Screen after 5 second halt '''
os.system('clear')
You can also try this code with Online Python Compiler
Run Code


Output:

Initially-

Initially

After 5 seconds-

Output after 5 seconds

Also see, Swapcase in Python, and Convert String to List Python

Clearing Screen when the Operating System is Unknown

If we don’t know what OS we are working on, in this case; first we need to determine the type of OS we are working on with the help of the code itself. 

For Windows, the os name is "nt".

For Linux or mac, the OS name is "posix".

Let’s understand how to clear screen in python in an unknown OS.

Method 1: Using the os.system() method 

'''Importing os and time module'''
import os
import time


'''We define our own clean function to clear the screen'''
def clean():
# For Windows
if os.name == 'nt':
_ = os.system('cls')


# For macOS and Linux
else:
_ = os.system('clear')


'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)


'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)


'''Clearing the Screen after 5 second halt '''
clean()
You can also try this code with Online Python Compiler
Run Code


Output:

Initially-

Initially

After 5 seconds:

Output after 5 seconds

Method 2: Using the call method of the subprocess module

'''Importing os, subprocess, and time module'''
import subprocess, os
import time


'''We define our own clean function to clear the screen'''
def clean():
    
# For macOS and Linux
    if os.name == 'posix':
        _ = subprocess.call('clear')


    # Windows
    elif os.name == 'nt':
        _ = subprocess.call('cls')


'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)


'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)


'''Clearing the Screen after 5 second halt '''
clean()
You can also try this code with Online Python Compiler
Run Code


Output:

Initially-

Initially

After 5 seconds-

Output after 5 seconds

How Shell of Python look like?

You can use Python IDLE as a shell or can use your computer terminal as a python shell.

The screenshot below shows the Python IDLE Shell.

Python idle shell

The screenshot below shows the computer terminal used as a Python shell.

Python shell

Read more about, Fibonacci Series in Python

What are the Commands to Work on Python Shell? 

There are three main ways to run Python shell commands:-

  • os.system()
     
  • subprocess.Popen() 
     
  • subprocess.run()
     

Let’s discuss the working of the above three methods.

os.system()

The os module provided by Python is one of the standard modules used to interact with the operating system. 

Let’s understand with examples the different commands we can run using the os.system() method.

Example 1:

In this example, we will use the os.system() method to execute the shell commands of echo.

# Importing os module
import os

os.system('echo "Coding Ninjas!"')
You can also try this code with Online Python Compiler
Run Code


Output:

terminal

Example 2:

In this example, we will use the os.system() method to execute the PWD shell script.

# Importing os module
import os

os.system('pwd')
You can also try this code with Online Python Compiler
Run Code


Output:

terminal

subprocess.Popen() 

The subprocess module is used to run new programs/applications. 

Let’s understand with examples the different commands we can run using the subprocess.Popen()  method.

Example:

In this example, we will use the subprocess.Popen() method to execute the shell commands of echo.

# Importing subprocess module
import subprocess

subprocess.Popen('echo "Coding Ninjas!"', shell=True)
You can also try this code with Online Python Compiler
Run Code


Output:

terminal

You can try it on online python compiler.

subprocess.run()

The subprocess module is used to run new programs/applications. 

Let’s understand with examples the different commands we can run using the subprocess.run()  method.

Example:

In this example, we will use the subprocess.run() method to execute PWD shell script.

# Importing subprocess module
import subprocess

subprocess.run("pwd", shell=True)
You can also try this code with Online Python Compiler
Run Code


Output:

terminal

You can also read about the Multilevel Inheritance in Python.

What is the clear command for Python Shell?

In an interactive shell/terminal, we can simply use ctrl+l to clear the shell/terminal. But what if we want to clear a command shell/terminal screen in Python while running a python script?

To do so, we need to write our own code/program. One of the methods to clear the shell/terminal is by using the clear command with the help of os.system() method.

Let’s understand how to clear screen in python using the clear command.  

'''Importing os and time module'''
import os
import time

'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)

'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)

'''Clearing the Screen after 5 second halt '''
os.system('clear')
You can also try this code with Online Python Compiler
Run Code


Output:

Initially-

terminal

After 5 seconds-

terminal

Must Recommended Topic, Floor Division in Python

Frequently Asked Questions

What is the shortcut for clear in Python?

The shortcut to perform clear in python is ctrl+l. Most of the time, we must clear the screen while the python script is operating, so we must do it manually or with the help of a program.

How do I clear only part of a screen in Python?

You can use the system(clear) method. Simply enter space characters in the screen lines you want to remove with the system("clear"). 

How do you clear the screen in Python VS code?

There is a shortcut to clear the screen in python. Simply press the Ctrl, Shift, and P keys concurrently to open the command palette in Visual Studio Code and enter the command Terminal: Clear.

Conclusion

In this article, we have extensively discussed different ways to clear a command shell/terminal screen in Python while running a python script. I hope you enjoyed this blog on How to clear screen in Python.
 

Live masterclass