Getting User Input in Python

If you're trying to create something in which you require input from a user, like a simple calculator, you can use the input method provided by Python.

Input function

Python has a built-in function - input([prompt]) to accept input from the user. If the prompt is passed it is written on the standard output otherwise it is blank. It is advised to show the prompt message.

String as input

input function always returns the value of string datatype. So explicit conversion to string data type is not required.

Numbers as input

As we know input always returns a string, so if we need numeric types, we have to cast them using appropriate data type.

Password as input

There may be cases when you want a user to enter the password, showing the input is not a good idea, in such cases, we can use getpass module.

We can see there is no input displayed on the screen but it gets stored in password variable.

Conclusion

In this tutorial, we discussed how to accept user input like username, number, password from the user for our Python program.

Happy Coding. Never Stop Hacking!!

0 Comments

Join discussion:

Login