How do you clear the Python interactive interpreter console screen? | A1Lab@MZeeshanM to PythonEnglish • 2 days ago

How do you clear the Python interactive interpreter console screen?

message-square
0

Press Ctrl + L in most modern terminals and Python 3.13+'s interactive shell (PyREPL) to clear the viewport without invoking a subprocess, or run os.system("cls" if os.name == "nt" else "clear") (wrapped in a zero-argument helper function or repr shortcut so os.system’s 0 exit status does not print to the console).

Basic Example

import os

# 1. Recommended (Cross-platform helper function — prints nothing extra):
def cls() -> None:
    os.system("cls" if os.name == "nt" else "clear")

cls()

# 2. Pure ANSI escape sequence (no `os` subprocess needed on modern terminals):
print("\033[H\033[2J", end="", flush=True)

Key Details & Edge Cases

  • Keyboard shortcut Ctrl + L (fastest way without typing Python code):
    • On macOS, Linux, Windows Terminal, and Python 3.13+ on all platforms (which introduced the built-in PyREPL interactive shell), pressing Ctrl + L clears the screen immediately while preserving your current variables and command history.
    • Note: In legacy Windows cmd.exe running Python $\le$ 3.12, Ctrl + L inserts a literal ^L character instead of clearing the screen; use os.system("cls") or upgrade to Windows Terminal / Python 3.13+.
  • Why bare os.system("clear") prints 0 in the REPL:
    • The Python interactive REPL automatically prints the return value of any top-level expression that is not None. Because os.system(...) returns the shell exit code 0, running os.system("clear") directly leaves a 0 at the top of the cleared screen. Assigning _ = os.system(...) or calling a None-returning helper function cls() prevents the trailing 0.
  • Jupyter Notebook / IPython (%clear or IPython.display.clear_output):
    • Inside IPython, type %clear (or press Ctrl + L).
    • Inside Jupyter Notebook / JupyterLab cells, os.system("clear") does nothing to cell output; call from IPython.display import clear_output; clear_output(wait=True) instead.
  • IDLE (Python’s built-in Tkinter IDE):
    • IDLE does not execute ANSI escape codes or os.system("cls") in its Shell window, and has no built-in clear-screen command prior to optional extensions (Shell $\rightarrow$ Restart Shell via Ctrl + F6 resets the session visually, though it also clears variables).
  • Auto-loading cls() on every REPL startup (PYTHONSTARTUP):
    • Save the cls() helper in a file such as ~/.pythonrc.py and set the environment variable PYTHONSTARTUP=~/.pythonrc.py so cls() is available automatically every time you launch python.

Comparison of REPL Screen-Clearing Methods

Method Preserves Active Variables & History? Prints Trailing 0 Return Value? Works on Windows, macOS & Linux? Best Used When
Ctrl + L keyboard shortcut (Recommended) Yes No Yes (Python 3.13+ everywhere; macOS/Linux/Windows Terminal on $\le$ 3.12) Clearing the terminal interactively without typing code.
def cls(): os.system("cls" if os.name == "nt" else "clear") Yes No (returns None) Yes (works even in legacy Windows cmd.exe) Programmatic clearing or adding a reusable cls() command via PYTHONSTARTUP.
print("\033[H\033[2J", end="", flush=True) Yes No Yes (all ANSI/VT100 terminals, including Windows 10/11 Terminal) Clearing the screen without spawning a child shell process.
clear_output(wait=True) (IPython.display) Yes No Yes (Jupyter Notebook / JupyterLab) Updating or wiping output cells inside Jupyter notebooks.