How to clear screen in python?
Last Updated :
12 Aug, 2024
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
Similar Reads
How to clear screen in python?
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+lBut, what if we want to clear the screen while running a python script? Unfort
4 min read
How to Get Coordinate Of Screen in Python Turtle ?
Turtle is a special feature in python which contains a graphical library. In this article we will learn how to Get Coordinate Of Screen in Python Turtle. Turtle has many built in function to create this program we use following. import turtle --> This is the python library which allow us to acces
1 min read
How to Remove All Items From a List in Python
Removing all items from the list can be done by using various different methods in Python. Using clear()clear() method is the simplest and most efficient way to remove all items from a list. [GFGTABS] Python a = [1, 2, 3, 4, 5] # Remove all items using clear() a.clear() # Print the empty list print(
2 min read
Python Script to Logout Computer
As we know, Python is a popular scripting language because of its versatile features. In this article, we will write a Python script to logout a computer. Letâs start with how to logout the system with Python. To logout your computer/PC/laptop only by using a Python script, you have to use the os.sy
2 min read
Python - Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read
Python - Remove Item from Dictionary
There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Python Script to Shutdown Computer
As we know, Python is a popular scripting language because of its versatile features. In this article, we will write a Python script to shutdown a computer. To shut down the computer/PC/laptop by using a Python script, you have to use the os.system() function with the code "shutdown /s /t 1" . Note:
1 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
How to Keep a Python Script Output Window Open?
We have the task of how to keep a Python script output window open in Python. This article will show some generally used methods of how to keep a Python script output window open in Python. Keeping a Python script output window open after execution is a common challenge, especially when running scri
2 min read
Python Script to Restart Computer
As we know, Python is a popular scripting language because of its versatile features. In this article, we will write a Python script to restart a computer. Let's start with how to restart the system with Python. Note: For this to work, you have to import os library in the ide. If you don't have it,
1 min read