Ask coding questions

← Back to all posts
clear
owenbaseball (1)

How do I clear only part of the screen in python

Answered by RYANTADIPARTHI (6048) [earned 5 cycles]
View Answer
Comments
hotnewtop
RYANTADIPARTHI (6048)

Do you mean only one part? If that's what you mean, then you can't do it. THe entire screen has to be cleared.

import os
os.system('clear')

or

clear()

Any of them should work

Bookie0 (6407)

@RYANTADIPARTHI

clear() won't just work like that, you have to put it in a function first

InvisibleOne (3227)

what part of the screen do you not want cleared?

Bookie0 (6407)

Using this:

import os 

os.system('clear') # clears all of the console (black part)

You can use it whenever you want, or put it into a function:

import os

def clear(): # defining the function 
  os.system('clear')

clear() # calls the function, and clears console

This will clear everything on the console, so if your wanted certain parts of your console cleared, that won't be possible unfortunately.

You could always just re-print the stuff you want to keep on the console, ie:

import os

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

# if you just want 'Hello there!' to be cleared
# but you want 'How are you?' to stay
# you could maybe output it again, but it would be kinda troublesome


print("Hello there!")

print("How are you?")

clear() # clears everything from console

print("How are you?") # outputs 'How are you?' again (change it to what you want to remain)


name = input("Enter your name ") # an input asking for your name
Coder100 (18934)

You can't. You can only clear all of the screen or none of the screen.

import os

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

print("Hello,")
input()
clear()
print("World!")
MikeJMS8910 (234)

if you want to clear the output un-manually you can put print("\033c")