How to Clear the Screen in Python

To clear the screen in Python, use os.system() to run your operating system’s terminal-clearing command. This works in a terminal, but IDLE, Jupyter notebooks, and IDE output panels need different approaches. The examples below cover each environment, including how to clear the display and return the cursor to the top.

What Clearing the Screen Means in Python

The right way to clear Python output depends on where the output appears, not just which operating system you use.

Python does not have a universal built-in function for clearing every kind of screen. A terminal interprets terminal commands and control sequences; a notebook or graphical output panel manages its own display.

Clearing the screen also differs from resetting Python:

  • Clearing the display removes visible output without deleting your variables, functions, or imports.
  • Restarting Python creates a new interpreter session and loses its in-memory state.
  • Clearing command history or saved logs is a separate operation. A cleared display does not guarantee that previous output is unrecoverable.

Methods named clear() on dictionaries or lists empty those containers, they do not clear the console.

How to Clear the Screen in a Python Terminal

Choose a clearing method based on the output environment:

Where Your Output Appears Appropriate Approach
A Windows terminal running Python Run the Windows clear-screen command through Python
A macOS or Linux terminal running Python Run the terminal’s clear-screen command through Python
An IDE’s integrated terminal Use the terminal method for its operating system
IDLE Shell or a graphical IDE output panel Use the application’s clearing control or start a fresh Shell
A Jupyter notebook cell Clear the cell’s output through IPython

Step 1: Confirm You Are Using a Terminal

  1. Check where your Python output appears. An IDE’s integrated terminal counts as a terminal; its debug console or output panel may not.
  2. Enter the following example as Python code, either in your script or at a terminal-based Python prompt. Do not paste the entire Python example directly into Command Prompt, PowerShell, or a Unix shell.

If your output is in a notebook or graphical Shell, use that environment’s dedicated approach instead of trying to force a terminal command to work.

Step 2: Run the Clear-Screen Function

    import os
    def clear_screen():
        if os.name == "nt":
            os.system("cls")    # Windows
        else:
            os.system("clear")  # macOS and Linux
    clear_screen()
    print("Ready.")
    1. After running the example, check that the previous output is gone and `Ready.` appears at the top of the terminal’s working area.

    The function selects the command using os.name. Windows reports "nt"; the other branch handles ordinary macOS and Linux terminal sessions.

    The os module is part of Python’s standard library, so you do not need to install a package. However, this function still relies on the surrounding operating system: the Unix-style clearing utility may be missing from a minimal container.

    The function clears the display without restarting Python. Variables and imports created before the call remain available afterward.

    Clear the Screen With ANSI Escape Sequences

    In an ANSI-compatible terminal, you can clear the visible display without launching an operating-system command:

    print('\33[2J\33[f', end='', flush=True)
    print("Ready.")

    Run the snippet and check that Ready. appears at the upper-left corner.

    The sequence contains two separate instructions:

    • \33[2J erases the visible display.
    • \33[f moves the cursor to the screen’s home position, the upper-left corner.

    Erasing the display does not necessarily reposition the cursor. Using the erase instruction alone can leave your next output partway down an otherwise blank screen.

    The end argument avoids adding an extra newline after the control sequence. The flush argument sends it immediately rather than leaving it in Python’s output buffer.

    This approach requires a terminal that interprets ANSI/VT control sequences. If strange characters appear instead of a cleared display, the output surface is not handling those sequences. Use its own clearing control or run the code in a compatible terminal.

    Do not treat this sequence as a way to erase saved logs, input history, or terminal scrollback.

    Clear Output in IDLE, Jupyter, and IDE Consoles

    IDLE, Jupyter, and IDE consoles manage their own displays, so clearing output requires an environment-specific approach.

    Python IDLE Shell

    IDLE’s Shell is a graphical text window, not a normal terminal. Operating-system clearing commands and ANSI escape sequences do not reliably clear its visible transcript.

    For a fresh Shell:

    1. Save any code you need to keep in an editor file.
    2. Close IDLE and reopen it.
    3. Rerun your saved script if you need its definitions again.

    Restarting the interpreter inside an existing IDLE Shell is not the same as removing the Shell’s displayed transcript.

    If your program needs to clear its screen while continuing to run, execute it in a terminal rather than IDLE’s Shell.

    Jupyter Notebook Output

    To clear the output associated with the currently executing notebook cell, run:

    from IPython.display import clear_output
    clear_output()
    print("Ready.")

    The cell’s previous output is replaced by Ready.. Its input code stays in place, and the kernel keeps its variables and imports.

    This is useful when a cell repeatedly displays progress or updated results. It does not clear every cell in the notebook; use the notebook interface’s output-clearing controls when that is your goal.

    IDE Debug Consoles and Output Panels

    An IDE can contain several different output surfaces. Its integrated terminal may support ordinary screen clearing, while its debug console or output panel may only display text.

    Use a graphical panel’s built-in clearing control if it provides one. If it does not, rerun the script in the IDE’s integrated terminal and use the operating-system method there.

    Use Blank Lines Only as a Workaround

    If an output surface cannot clear its display, you can print 30 blank lines to push earlier output upward:

    print("\n" * 30, end="")
    print("New output starts here.")

    The empty end argument prevents print() from adding another newline after the requested blank lines.

    This is a visual separator, not a true screen clear. It does not erase previous text, and a tall window may still show some of that text. New output appears after the blank lines rather than reliably starting at the top of a freshly cleared screen.

    Use this workaround only when separating old and new output is sufficient.

    Confirm the Clear Worked and Keep Logs Readable

    A successful terminal clear should leave the display ready for new output without interrupting your Python program. Check that:

    • Old output no longer occupies the visible working area.
    • New output begins at the expected cursor position.
    • The program continues without recreating its variables or imports.
    • No raw escape characters appear in the display.

    For scripts that also run in containers, automation, or redirected sessions, check whether standard output is connected to a terminal:

    import sys
    print(sys.stdout.isatty())

    A False result means standard output is not connected to a terminal. In that situation, leave output as ordinary lines rather than emitting display-control sequences.

    A True result confirms a terminal connection, but it does not guarantee support for every ANSI sequence.

    If a Linux terminal reports that its clearing utility is missing, the ANSI method avoids that external dependency, provided the terminal supports it. Installing a clearing utility will not make a log viewer or graphical output panel behave like a terminal.

    Once clearing works, place the clear operation immediately before the fresh output you want to display. Keep automated runs as readable logs rather than trying to make them look like an interactive screen.

    Frequently Asked Questions

    Is There a Clear Command in CMD?

    Yes, Windows Command Prompt uses this command to clear its window:

    cls

    Enter it at the Command Prompt, not at Python’s interactive prompt. Inside Python, the command must be invoked through an operating-system interface such as os.system().

    How Do I Quit Python Interactive Mode?

    To leave Python’s interactive interpreter and return to your operating-system shell, enter:

    exit()

    This ends the Python session rather than simply clearing its display. Any variables and imports held only in that session are lost.

    How Do I Clear the Turtle Screen in Python?

    In an existing Turtle program or interactive session, use Turtle’s screen-clearing function:

    import turtle
    turtle.clearscreen()

    This deletes all drawings and turtles and resets the screen to its initial state. It affects the Turtle graphics window, not the terminal or notebook output.