Hello guys 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.

Hello guys 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.

Best 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 '\033c'") should get rid of scroll back too. Something that works on all systems:

import os
os.system('cls' if os.name == 'nt' else "printf '\033c'")