Skip to content

How to clear screen (console) in Python3

programming-banner

There are three ways to clear screen/console in Python3:

using print

print("\033c", end='')

using os

Clearing the output screen or console screen in Python3 can be done by using the os module.

You can use the os.system('clear') function to clear the console screen. This function will work on Unix/Linux/MacOS terminals.

from os import system, name

def clear():
    # for windows
    if name == 'nt':
        _ = system('cls')
    # for mac and linux(here, os.name is 'posix')
    else:
        _ = system('clear')

clear()

Note that the os.system() function is not very secure, and can be used to execute arbitrary commands. So be careful when using it.

using os and call

import os
from subprocess import call
def clear():
    _ = call('clear' if os.name == 'posix' else 'cls')

clear()
Leave your message