Skip to content
Advertisement

Is there a way to clear your printed text in python?

I have wanted for a long time to find out how to clear something like print(“example”) in python, but I cant seem to find anyway or figure anything out.

print("Hey")
>Hey

Now I need to clear it, and write some new text.

print("How is your day?")

It would print.

Hey

>How is your day?

But I want to clear the “Hey” so the user shouldnt look at both at same time, and it looks kinda messy.

Advertisement

Answer

import os
os.system('cls')

Or os.system('clear') on unix (mac and linux). If you don’t want the scroll up either, then you can do this:

os.system("printf '33c'") should get rid of scroll back too. Something that works on all systems:

import os
os.system('cls' if os.name == 'nt' else "printf '33c'")
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement