The Wayback Machine - https://web.archive.org/web/20250114213148/https://www.geeksforgeeks.org/getpass-and-getuser-in-python-password-without-echo/
Open In App

getpass() and getuser() in Python (Password without echo)

Last Updated : 15 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

getpass() prompts the user for a password without echoing. The getpass module provides a secure way to handle the password prompts where programs interact with the users via the terminal.

getpass module provides two functions :

Using getpass() function to prompt user password

Syntax: getpass.getpass(prompt=’Password: ‘, stream=None) 

The getpass() function is used to prompt to users using the string prompt and reads the input from the user as Password. The input read defaults to “Password: ” is returned to the caller as a string.

Example 1 : No Prompt provided by the caller 

Here, no prompt is provided by the caller. So, it is set to the default prompt “Password”. 

Python




# A simple Python program to demonstrate
# getpass.getpass() to read password
import getpass
 
try:
    p = getpass.getpass()
except Exception as error:
    print('ERROR', error)
else:
    print('Password entered:', p)


Output : 

$ python3 getpass_example1.py
Password: 
('Password entered:', 'aditi')

Example 2: Security Question 

There are certain programs that ask for security questions rather than asking for passwords for better security. Here, the prompt can be changed to any value. 

Python




# A simple Python program to demonstrate
# getpass.getpass() to read security question
import getpass
 
p = getpass.getpass(prompt='Your favorite flower? ')
 
if p.lower() == 'rose':
    print('Welcome..!!!')
else:
    print('The answer entered by you is incorrect..!!!')


Output :  

$ python3 getpass_example2.py

Your favorite flower?
Welcome..!!!

$ python3 getpass_example2.py

Your favorite flower?
The answer entered by you is incorrect..!!!

Using getuser() function for displaying username

Syntax: getpass.getuser()

The getuser() function displays the login name of the user. This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first non-empty string. 

Python




# Python program to demonstrate working of
# getpass.getuser()
import getpass
 
user = getpass.getuser()
 
while True:
    pwd = getpass.getpass("User Name : %s" % user)
 
    if pwd == 'abcd':
        print "Welcome!!!"
        break
    else:
        print "The password you entered is incorrect."


Output : 

$ python3 getpass_example3.py

User Name : bot
Welcome!!!

$ python3 getpass_example3.py

User Name : bot
The password you entered is incorrect.

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg