简体   繁体   中英

How to hide input() in Python 3.6?

After pressing enter the input stays there.For example:

myName = input()
print("My name is:" + myName)

and output would be:

Alex
My name is:Alex

But I want to display only the latter.I tried using getpass but it it necessary to show the text while typing and getpass hides it.What can I do?

import sys

myName = input()

sys.stdout.write("\033[F") # Cursor up one line

print("My name is:" + myName)

Depending on platform, you can move the cursor up a line before printing:

myName = input()
print("\x1B[F\x1B[2K", end="")
print("My name is:" + myName)

The middle line moves thw cursor up and clears that line in POSIX terminals.

On Windows, it's getting more complicated and you need some libraries to call Windows APIs to achieve this.

import getpass

password = getpass.getpass("Entering password: ")

print(password)

put both statements into one!

myName = input("My Name is: ")

This way, "My name is Alex" will be printed for Alex input. Also, Alex is assigned to "myName".

EDIT: Obviously, this is only a workaround and does not answer your question specifically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM