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:
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 print(getpass.getuser())
getpass.getuser()it gets the current user of the system. i.e root
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)
getpass.getpass()get the value of the password as a string.
- Line#7: It will print the input password on the console.