2

In python is it possible to erase the user's input line? For example:

msg = input("")
print("User: "+msg)

Output (Of the comp) :

Hey
User: Hey

Output (I want) :

User: Hey

There is an another question quite similar to this but it doesn't work for me..... Prevent Python from showing entered input

Update: A few details, I want to check the Users input before printing it, I even have a thread running in the same file that prints statements at random intervals. (This code is actually part of a video game chat)

1 Answers1

0

Just write:

print("User: "+ input(""))

Update: if you want to store the input so just do this:

print("User: ", end='')
msg = input("")

the print() function puts "\n" at the end of the string, so by replacing "\n" with "" we prevent python to jump to the next line and the user could write after printed text, "User: ".


Update 2: you can build a user interface for your program and it makes your work so easy. However, if you want to run your program on the console, the easiest way is to store all the printed strings in the console then clear the console each time the user inputs a msg and then print stored strings. Now you have the msg which is not printed in the console, so you can do whatever you want with it and then print it as print('User: ' , msg).

to clear the console:

import os

clear = os.system('cls') #for Windows
clear = os.system('clear') #for Linux

You can write clear like clear = lambda: os.system('cls') and use it like clear() whenever you want. But it return 0 and print a '0' in your console.

a full example:

import os

def clear():
    dummy_var = os.system('cls')

stored_string = [f'stored {i}' for i in range(10)]

for txt in stored_string:
    print(txt)

msg = input("")

clear()

for txt in stored_string:
    print(txt)

print('User: ', msg)
stored_string.append('User: ' + msg)

run this example and see the result.