- Make a system call with ‘clear’ in Linux and ‘cls’ in Windows as an argument.
- Store the returned value is an underscore or whatever variable you want (an underscore is used because python shell always stores its last output in an underscore).
# 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.name26 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()