Open In App

How to clear screen in python?

Last Updated : 12 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Most of the time, while working with Python interactive shell/terminal (not a console), we end up with a messy output and want to clear the screen for some reason. In an interactive shell/terminal, we can simply use

ctrl+l

But, what if we want to clear the screen while running a python script? Unfortunately, there’s no built-in keyword or function/method to clear the screen. So, we do it on our own.

Clearing Screen in windows Operating System

Method 1: Clear screen in Python using cls

You can simply “cls” to clear the screen in windows.

Python
import os

# Clearing the Screen
os.system('cls')

Example 2: Clear screen in Python using clear

You can also only “import os” instead of “from os import system” but with that, you have to change system(‘clear’) to os.system(‘clear’). 

Python
# import only system from os
from os import system, name

# import sleep to show output for some time period
from time import sleep

# define our clear function
def clear():

    # for windows
    if name == 'nt':
        _ = system('cls')

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

# print out some text
print('hello geeks\n'*10)

# sleep for 2 seconds after printing output
sleep(2)

# now call function we defined above
clear()

Example 3: Clear screen in Python using call

Another way to accomplish this is using the subprocess module

Python
# import call method from subprocess module
from subprocess import call

# import sleep to show output for some time period
from time import sleep

# define clear function
def clear():
    # check and make call for specific operating system
    _ = call('clear' if os.name == 'posix' else 'cls')


print('hello geeks\n'*10)

# sleep for 2 seconds after printing output
sleep(2)

# now call function we defined above
clear()

Clearing Screen in Linux Operating System

In this example, we used the time module and os module to clear the screen in Linux os.

Python
import os
from time import sleep

# some text
print("a")
print("b")
print("c")
print("d")
print("e")
print("Screen will now be cleared in 5 Seconds")

# Waiting for 5 seconds to clear the screen
sleep(5)

# Clearing the Screen
os.system('clear')


How to clear screen in python? – FAQs

How to Clear Screen in Python in Code?

To clear the screen in a Python script running in a command line or terminal, you can use the os module to execute the relevant command for your operating system:

import os

def clear_screen():
# For Windows
if os.name == 'nt':
_ = os.system('cls')
# For macOS and Linux
else:
_ = os.system('clear')

clear_screen()

This function checks the operating system and executes cls for Windows or clear for Unix-based systems (Linux and macOS).

How Do I Clear the Screen GUI in Python?

For clearing a graphical user interface (GUI) in Python, it depends on the GUI toolkit you are using. For example, in Tkinter, you might clear the contents of widgets or reset them. Here’s a simple example of how to clear a text widget in Tkinter:

import tkinter as tk

def clear_text():
text_widget.delete('1.0', tk.END)

root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
tk.Button(root, text="Clear", command=clear_text).pack()
root.mainloop()

This sets up a window with a text box and a button that clears the text box when clicked.

How to Clear Screen in Python PyCharm?

Clearing the screen in the PyCharm console isn’t straightforward through a Python script because standard clear screen commands (cls or clear) affect the system terminal and may not work in the integrated development environment (IDE) console like PyCharm. Instead, you typically need to manually clear the console using the buttons provided in the PyCharm interface, as there’s no built-in command in PyCharm to clear the console programmatically.

What Does turtle.clear() Do in Python?

Assuming t is a Turtle object in the Python Turtle graphics module, t.clear() clears the drawings on the screen done by the turtle instance but does not move the turtle. The screen remains intact, and the turtle state is unchanged. Here’s how you might use it:

import turtle

t = turtle.Turtle()
t.forward(100)
t.clear()

This would draw a line and then remove it without moving the turtle back to its starting position.

How to Clear Input in Python?

In Python, once you’ve taken input using the input() function, you can’t “clear” it in the sense of removing it from memory immediately as inputs are stored in variables. You can overwrite the variable or delimit its scope to ensure it’s no longer accessible:

user_input = input("Enter something: ")
# Process the input
print("You entered:", user_input)

# Clear by overwriting
user_input = None

If your goal is to ignore a user’s input under certain conditions and prompt them again, you’d typically use a loop that continues to request input until a valid condition is met.

Each of these methods addresses different types of “clearing” in Python, tailored to the environment or framework being used



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: