Depending on the OS, how you get one character from user input and how to check for carriage returns will vary.
See this post: Python reads one character from the user
In OSX, for example, you can do something like this:
import sys, tty, termios def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch key = "" sys.stdout.write('Password :: ') while True: ch = getch() if ch == '\r': break key += ch sys.stdout.write('*') print print key
source share