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
PyREPLinteractive shell), pressingCtrl + Lclears the screen immediately while preserving your current variables and command history. - Note: In legacy Windows
cmd.exerunning Python $\le$ 3.12,Ctrl + Linserts a literal^Lcharacter instead of clearing the screen; useos.system("cls")or upgrade to Windows Terminal / Python 3.13+.
- On macOS, Linux, Windows Terminal, and Python 3.13+ on all platforms (which introduced the built-in
- Why bare
os.system("clear")prints0in the REPL:- The Python interactive REPL automatically prints the return value of any top-level expression that is not
None. Becauseos.system(...)returns the shell exit code0, runningos.system("clear")directly leaves a0at the top of the cleared screen. Assigning_ = os.system(...)or calling aNone-returning helper functioncls()prevents the trailing0.
- The Python interactive REPL automatically prints the return value of any top-level expression that is not
- Jupyter Notebook / IPython (
%clearorIPython.display.clear_output):- Inside IPython, type
%clear(or pressCtrl + L). - Inside Jupyter Notebook / JupyterLab cells,
os.system("clear")does nothing to cell output; callfrom IPython.display import clear_output; clear_output(wait=True)instead.
- Inside IPython, type
- 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 ShellviaCtrl + F6resets the session visually, though it also clears variables).
- IDLE does not execute ANSI escape codes or
- Auto-loading
cls()on every REPL startup (PYTHONSTARTUP):- Save the
cls()helper in a file such as~/.pythonrc.pyand set the environment variablePYTHONSTARTUP=~/.pythonrc.pysocls()is available automatically every time you launchpython.
- Save the
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. |
You must log in or register to comment.
