2

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!

Noah Brown
  • 123
  • 1
  • 3
  • 8
  • 1
    Do you need this to be cross-platform? If not, which platform do you care about? – abarnert Aug 31 '18 at 15:12
  • Kind-of duplicate of [How to clear the interpreter console?](https://stackoverflow.com/q/517970/953482). The question there seems to specifically be about the REPL, which you're not using, but most of the answers apply equally well to an ordinary terminal as well. – Kevin Aug 31 '18 at 15:16
  • I was working on a text-based adventure game, so it would be for ios and android – Noah Brown Aug 31 '18 at 15:44

3 Answers3

1

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.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
0

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.

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

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.

Keir
  • 752
  • 5
  • 8