Masking user input in python with asterisks

Jackson Blankenship picture Jackson Blankenship · Dec 24, 2014 · Viewed 29.2k times · Source

I am trying to mask what the user types into IDLE with asterisks so people around them can't see what they're typing/have typed in. I'm using basic raw input to collect what they type.

key = raw_input('Password :: ')

Ideal IDLE prompt after user types password:

Password :: **********

Answer

Al Sweigart picture Al Sweigart · Feb 9, 2019

If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the stdiomask module:

pip install stdiomask

Unlike getpass.getpass() (which is in the Python Standard Library), the stdiomask module can display *** mask characters as you type.

Example usage:

>>> stdiomask.getpass()
Password: *********
'swordfish'
>>> stdiomask.getpass(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> stdiomask.getpass(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> stdiomask.getpass(mask='') # Don't display anything.
Password:
'swordfish'

Unfortunately this module, like Python's built-in getpass module, doesn't work in IDLE or Jupyter Notebook.

More details at https://pypi.org/project/stdiomask/