Python getpass Explained With Examples

Hello geeks, and welcome! In today’s article, we will cover Python getpass(). We will also look at theĀ definition and applicationĀ of this method. We will do all that with the help of examples.Ā It is a fascinating topic and something that we come across in our daily lives. Before moving ahead, let us first try to overview this topic.

Like NumPy and Matplotlib, getpass is also a module of Python. The getpass module asks the user for a password without echoing. It means without showing what the user is typing. As we move ahead in this article, we will cover various things. Besides this, we will also look at the getuser() and compare them.

Exploring getpass module and getpass() Function

In this section, we will explore the getpass() module. Let us start by giving a look at the syntax first.

getpass.getpass(prompt='Password: ', stream=None) 

This function generally reads user input as a password and returns it to the caller as a string. Let us look at some examples to help us understand things better.

1. Basic example of getpass

import getpass

try:
    pas = getpass.getpass()
except Exception as tion:
    print('Error Occured : ', tion)
else:
    print('Entered secret :', pas)

Output and Explanation:

getpass()

Above, we can see a fundamental example of getpass. Our code’s main objective was to ask for input from the user. To do this, we have first imported the getpass module. After this, we used the try and except command. They are used to handle exceptions. Following the try statement, the code is executed as a normal program. The command that follows the except statement responds to the try statement’s exception. It was an elementary example that let us look at something more complicated next.

2. Custom Questions

import getpass

pas = getpass.getpass(prompt = 'What is your favourite food?')
if pas == 'Butter chicken':
    print('Delicious!')
else:
    print('Wrong Answer')

Output and Explanation:

getpass()

Setting a custom security question is something that we will all do, from social media sites to online banking and much more. In case you forget your password, these security questions come to your rescue. A similar thing can be executed with the help of a getpass.

In the getpass syntax, we have specified the prompt, which is our question. Then, we have specified the answer to it. After the code runs, the program asks the user for the input. If the 2 matches, you get delicious printed out. In the next section, let us look at the getuser().

Exploring getuser() Function of getpass Module

This section will look at another function of the getpass() module. What this function does is print the user name and other login credentials. Let us look at an example that will make things clearer.

import getpass

user = getpass.getuser()

while True:
    pas = getpass.getpass("User Name : %s" % user)
    if pas == 'Rohit':
        print("Welcome!!!")
        break
    else:
        print("The password you entered is incorrect.")

Output and Explanation:

getuser()

Above, we can see an example of getuser(). To execute this, we first import the getpass module. After which, we getuser tag; what it does is that it gets the user name from the database. In my case, it gets KIIT. Then, we have to use the getpass comment and specify the password. If the user input matches it, then the login is successful.

The primary difference between the getpass and getuser functions is that getuser also brings in the username name and login credentials. In the case of getpass, we can set passwords and security questions.

Conclusion

In this article, we coveredĀ Python getpass(). It is present in the getpass module of Python. We also covered its definition and syntax. To understand the thing properly, we looked at a couple of examples. In the end, we also compared it with another function named getuser.

I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read a byte-like error next?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments