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 a couple of examples that will help us understand things better.

1. Basic example of getpass

import getpass

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

Output and Explanation:


Above, we can see a fundamental example of getpass. Our code’s main objective was to ask the input from the user. To do this, we have first imported the getpass module. After which, we have used the Try and except command. They are used to handle exceptions. Following the try, statement 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('Wrong Answer')

Output and Explanation:


Setting a custom security question is something that we will all do, from social media sites to online banking to 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 for it. After the code runs, the program asks the user for the input. If the 2 matches, you get delicious printed out. Now in the next section, let us look at the getuser().

Exploring getuser() Function of getpass Module

In this section, we will look at another function of the getpass() module. What this function does is that prints 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("The password you entered is incorrect.")

Output and Explanation:


Above, we can see an example of getuser(). To execute this, at first, we 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 function is that getuser also brings in the username name and login credentials. Whereas in the case of getpass, we can set and password and security questions.


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 in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read byte like error next

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments