Ask coding questions

← Back to all posts
How do you make a clear() function?
Imthebestthe (108)

I was making a peice of work with some of my freinds and i was wondering how i could make a clear funcion. because i cant seem to find one that clears all of the text in the terminal or one that looks nice. could you please help and thanks for your time!
edit: I would just like to say, it may not be pssible to clear the text in terminal but i would like it to look like it did . Thanks also, @Bookie0 could you help lmao.

Comments
hotnewtop
OlauPla (159)

There is a better way:

def clear(seconds = 0):
 time.sleep(seconds)
 os.system('clear')

Then you use it like this:

clear()

Or type inside the parenthesis the seconds you want it to wait

clear(2)

Number 2 is only an example

Now you can make it wait before clearing if you want too!

Hope it helped

SUHASTADIPARTH1 (191)

You just have to do this:

import os

print('something')
os.system('clear') # clears the screen after printing something

or

clear_system = lambda: os.system('clear')

# then call the clear_system() function anywhere in your program

print('something.......')
clear_system()
Bookie0 (6359)

like this:

import os #importing os

def clear(): # putting it in a function so you can use it many times
  os.system('clear') # clears console

# to use it:

clear() # clears the console

Easy! :)

RYANTADIPARTHI (6018)

You don't need anything

Clear is already a function in python.

words = {'a', 'b', 'c'}

words.clear()

That will work

[deleted]

Just do what everyone else is telling you to do:

def clear():
  import system
  os.system("clear")

And to use it just do:

import time
print("This text will disappear in 5 seconds!")
time.sleep(5)
clear()
Bookie0 (6359)

to clear the console:

import os

os.system('clear')

# to make it in a function

def clear():
     os.system(‘clear’)

clear() # class the function

(from my tutorial here)! ;)

[deleted]

@Bookie0 You misspelled import. Why am I correcting other peoples code now.

Imthebestthe (108)

hey, do you have discord? please join this server and msg the guy named kars. You can leave afterwords if you want
@Bookie0

Bookie0 (6359)

@Imthebestthe please don't advertise discord servers... =/

Imthebestthe (108)

i wasnt advertising i just want to talk to u on discord and thats the only way i know but wsorry
@Bookie0

Bookie0 (6359)

@Imthebestthe you can friend me on discord Bookie0#7538 of you want :)

Coder100 (18820)

Simple

import os

def clear():
  os.system("clear")

And then to use it:

clear()

easy, right?