Skip to content

Latest commit

History

History
33 lines (19 loc) 路 1.2 KB

File metadata and controls

33 lines (19 loc) 路 1.2 KB

The getpass Module

References:

Problem

When using the input function in Python to capture user inputs, we end up seeing the resulting value as the user types it (in this case "vanilla bean"):

fav_flavor = input("Please input your favorite icecream flavor: ")

#> Please input your favorite icecream flavor: vanilla bean

But what if we want to ask for more sensitive inputs, like secret passwords or credentials? We need a way to prevent them from showing up in the program's output.

Solution

We can hide a user-inputted value by using the getpass function instead of the input function. The getpass function will either hide the resulting value, or mask it using a key icon or series of asterisks:

from getpass import getpass

my_password = getpass("Please input your secret password: ")
#> Please input your secret password: 馃攽

This is espectially helpful in Google Colab, so you can share your notebook's output without exposing your secret credentials:

Screen Shot 2022-02-23 at 9 40 25 PM