how to clear terminal python code example

Example 1: how to clear console python

import os
os.system('cls' if os.name == 'nt' else 'clear')

Example 2: how to clear the console python

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

Example 3: clearing the terminal in python

import os
os.system("clear")
print("Hello!")
#Only Hello will be in the terminal

Example 4: how to clear terminal with python

#pip install os
#pip install time
#pip install colorama
#pip install tqdm
import os
from tqdm import tqdm 
from colorama import Fore, Back, Style 
import time

for i in tqdm (range (101),  
               desc=Fore.GREEN + "Loading. . .",  
               ascii=False, ncols=75): 
    time.sleep(0.01)       

#the Fore.GREEN adds colour to the loading bar
print(Fore.GREEN + "Complete. . .") 
time.sleep(1) 
#this will clear the terminal
os.system('cls' if os.name == 'nt' else 'clear')