What is os.getpass() in Python?

The os.getpass() function in Python is used for security purposes. It allows user to type their password without echoing it. This means it does not display the typed characters.

It is used to secure the user’s credentials on the underlying application. In this way, the user’s security is maintained without compromising it. The getpass module has two methods:

  1. getpass.getuser()
  2. getpass.getpass()

1. getpass().getuser()

This method is used to get the input username as it is hidden with encryption and some secret passwords are used to keep the usernames secure.


#Import getpass module
import getpass
#Print the entered username
  • Line#4: getpass.getuser() it gets the current user of the system. i.e root

2. getpass.getpass()

The getpass.getuser() method is used to get the value of the password as a string as it is secure with encryption.


# Import getpass module
import getpass
# Password variable holds the value 
# of the password as string
password = getpass.getpass()
# Print the password
print("You entered",password)
  • Line#5: getpass.getpass() get the value of the password as a string.
  • Line#7: It will print the input password on the console.

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...