How to Clear Console in Python — pythonpip.com

Parvez Alam
1 min readSep 4, 2022

--

This tutorial helps How to Clear Console in Python. There is a number of ways to clear the console based on the operating system. You can also clear the interpreter programmatically.

We’ll use os library, which is a built-in library that comes with Python3 installations.

Python clear console

Import the os module and use its os.system() function to wipe the console in Python.

Let’s import os module:

Linux system

We’ll use the os.system('clear') command to clear the console.

The Sample code:

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

The screen will be cleared if the above command is entered into your console.

Windows system

For the Windows system, We’ll use the os.system('cls') command to clear the console.

The Sample code:

import os def clear_console(): os.system('cls') clear_console()

Run the above command in your terminal to clear the screen.

Lambda Function To Clear Console in Python

You can also use the lambda function to clear the python console.

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

The screen will be cleared if the above code will run.

Originally published at https://www.pythonpip.com on September 4, 2022.

--

--

Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I love learning and sharing knowledge. https://www.phpflow.com/