← Go Back

How to Clear the Terminal/Console/Screen

To clear the console in Python, use the standard os.system() function.

On Windows:

import os
os.system("cls")

On Linux or macOS:

import os
os.system("clear")

You can create a simple cross-platform function by checking the value of os.name:

import os

def clear_console():
"""
Clear the console on Windows, Linux and macOS.
"""
if os.name == "nt":
os.system("cls")
else:
os.system("clear")

clear_console()


console terminal os


🐍 You might also find interesting: