
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to clear Python shell?
Python provides a python shell which is used to execute a single Python command and display the result. It is also called REPL. REPL stands for Read, Evaluate, Print and Loop. The command is read, then evaluated, afterwards the result is printed and looped back to read the next command.
Sometimes after executing so many commands and getting haphazard output or having executed some unnecessary commands, we may need to clear the python shell. If the shell is not cleared, we will need to scroll the screen too many times which is inefficient. Thus, it is required to clear the python shell.
The commands used to clear the terminal or Python shell are cls and clear.
In Windows
import os os.system(‘CLS’)
In Linux
import os os.system(‘clear’)
Let us suppose, we need to show some output for few seconds and then we want to clear the shell. The following code serves the purpose.
Import os and sleep
Define a function where the commands to clear the shell are specified, cls for windows and clear for linux.
Print some output
Let the screen sleep for few seconds
Call the function to clear the screen
Example
from os import system, name from time import sleep def clear(): # for windows if name == 'nt': _ = system('cls') # for mac and linux else: _ = system('clear') print(“Hi Learner!!”) sleep(5) clear()
- Related Articles
- How to clear screen using python?
- How to clear screen in python?
- How to execute a Python file in Python shell?
- Python Interface to Shell Pipelines
- How to know/change current directory in Python shell?
- Python Program to Implement Shell Sort
- How to clear the text entered in Selenium with python?
- set clear() in python
- How to clear Tkinter Canvas?
- 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 to clear screen using C#?
- How to clear screen using Java?
