1

I have created a simple login program using python, I would like it to display stars or dashes when the user enters a password instead of displaying numbers.

Fred NGOMA
  • 19
  • 5
  • You'll have to read and react to one character at a time. See e.g. https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user – Erik Cederstrand Jan 31 '22 at 10:40

2 Answers2

3

If you just want to avoid echoing the input - you can use a python standard library called getpass- in it, there is a function called getpass which hides the user input:

from getpass import getpass
password = getpass()

Note that getpass requires a proper terminal, so it can turn off the echoing of typed characters.

If you wish to display stars when the user enters a password, you can install the pwinput module:

pip install pwinput

For example:

>>> pwinput.pwinput()
Password: ********
'password'

You can also change the mask character:

>>> pwinput.pwinput(mask='X') # Change the mask character.
Password: XXXXXXXX
'password'

And you can also change the prompt:

>>> pwinput.pwinput(prompt='PW: ', mask='*') # Change the prompt.
PW: ********
'password'
2

The PWInput module is able to display characters as you type.

>>> import pwinput
>>> pwinput.pwinput()  # Show * for each typed character.
Password: *********
'swordfish'
>>> pwinput.pwinput(prompt='PW: ')  # Show a custom prompt.
PW: *********
'swordfish'
>>> pwinput.pwinput(mask='X')  # Show a different character when user types.
Password: XXXXXXXXX
'swordfish'
>>> pwinput.pwinput(mask='') # Don't show anything when user types (falls back and calls getpass.getpass()).
Password:
'swordfish'

Have a look! https://pypi.org/project/pwinput/1.0.1/

yannvm
  • 418
  • 1
  • 4
  • 12