Python: hiding input characters with Stars

557 views Asked by At

I am requesting user input. I am asking for username then password. EG

username = input('Please enter a Username:\n')
password = input('Please enter a Password:\n')

When run this will of course appear like this:

Please enter a Username:
Iain
Please enter a Password:
Iain

What I want is this:

Please enter a Username:
Iain
Please enter a Password:
****
1

There are 1 answers

0
Agni On

You can use a library called pwinput.

You can install it using pip: pip3 install pwinput.

And use it as follows:

import pwinput
password = pwinput.pwinput()

This would open a prompt like this:

Password: ****

If you want to remove the text 'Password:', you can do:

password = pwinput.pwinput(prompt='')