Simple Python Function for Clearing the Command Line Interface - in Windows

Simple Python Function for Clearing the Command Line Interface - in Windows

The terminal or Command Line Interface (CLI) is a powerful tool for developers, system administrators and power users. It allows you to perform a variety of tasks quickly and efficiently, without the need for a graphical user interface (GUI). The terminal is also a great place to run Python scripts and explore the language's capabilities. However, when working in the terminal, you may sometimes find that you need to clear the screen to better organize your workflow.

On Linux and macOS systems, the standard keyboard shortcut to clear the terminal is "ctrl + l". This command works flawlessly and is easy to remember. Unfortunately, Windows systems don't support this shortcut, leaving Windows users searching for alternative solutions.

If you're a Windows user and you're looking for a simple way to clear the terminal, there's a simple workaround that can help. By writing a simple Python function, you can quickly and easily clear the terminal whenever you need to. Here's the code for the function:

def cls():
    import os

Once you have this function saved in your Python environment, you can easily clear the terminal by simply calling it. Make sure you're already running the Python terminal, and then simply type the following command:


This simple function will clear the terminal, allowing you to better organize your workflow and keep your terminal clutter-free. Whether you're a seasoned Python developer or just starting out, this tip can help you work more efficiently in the terminal and make the most of your Python experience.