
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to clear screen in python?
In Python sometimes we have link the output and we want to clear the screen in the cell prompt we can clear the screen by pressing Control + l . But there are situations when we need to clear the screen programmatically depending on the amount of output from the program and how we want to format the output. In such case we need to put some commands in the python script which will clear the screen as and when needed by the program.
We need the system() from the OS module of python to clear up the screen. For different platforms like windows and Linux we need to pass different commands as shown in the example below. Also we use the ‘_’ variable which is used to hold the value of last expression in interpreter.
Example
import os from time import sleep # The screen clear function def screen_clear(): # for mac and linux(here, os.name is 'posix') if os.name == 'posix': _ = os.system('clear') else: # for windows platfrom _ = os.system('cls') # print out some text print("The platform is: ", os.name) print("big output\n"* 5) # wait for 5 seconds to clear screen sleep(5) # now call function we defined above screen_clear()
Output
Running the above code gives us the following result −
The platform is: nt big output big output big output big output big output
The above output gets cleared after 5 seconds from the result window.
- Related Articles
- How to clear screen using python?
- How to clear screen using C#?
- How to clear screen using Java?
- How to Clear Linux terminal screen?
- How to clear Python shell?
- How to print to the Screen using Python?
- How to clear the text entered in Selenium with python?
- set clear() in python
- Printing to the Screen in Python
- Different ways to clear a list in Python
- How to clear console in C?
- How to clear console in MongoDB?
- How to clear ArrayBlockingQueue in android?
- How do I clear the regular expression cache in Python?
- How to clear Tkinter Canvas?
