Reputation: 123
I want to clear the console after each print/input statement to clean the screen up. I don't want to just add 100 blank lines, but actually clear the console so it's blank before the next print statement.
import random
import time
input("Hello (click enter to continue after each line)")
# I want to clear the console here so it is blank before it prints again
print ("What is your name?")
# At this point, the line above is the only thing on the screen
lower_username = str(input("Enter Username: "))
upper_username = lower_username.capitalize()
input("Hello " + upper_username)
Thanks for the help!
Upvotes: 2
Views: 1719
Reputation: 860
This isn't quite 'clearing the console' but one way to do a similar thing is using the \r character. This is called the carriage return and moves the cursor to the start of the line.
print('Line one\r', end='', flush=True)
print('Line two\r', end='', flush=True)
The above would print on the same line. Note that if the first string is longer than the other, then the end of it will still show. You can visually get around this by adding whitespace to the shorter string. For example:
print('Line long\r', end='', flush=True)
print('Line two\r', end='', flush=True)
Would output Line twog
because the first string is longer by one character. The visual fix would be:
print('Line long\r', end='', flush=True)
print('Line two \r', end='', flush=True)
Note the extra whitespace after 'two'.
Adding end='' stops the print function adding a newline \n character to the end of the string, enabling you to print to the same line.
Upvotes: 0
Reputation: 61900
Clearing the interpreter is platform dependent, but you can find out about the underlying OS:
import os
import platform
def clear():
if platform.system() == 'Linux':
os.system('clear')
elif platform.system() == 'Windows':
os.system('cls')
input("Hello (click enter to continue after each line)")
clear()
print("What is your name?")
# At this point, the line above is the only thing on the screen
lower_username = str(input("Enter Username: "))
upper_username = lower_username.capitalize()
input("Hello " + upper_username)
The answer is based in these two 1 and 2.
Upvotes: 1
Reputation: 29071
That is not in python's hands. Python can only print somewhere. it does not know where it's printing. If you are in a linux-like terminal, print("\033c")
could work.
Upvotes: 0