Using input() and getpass() for user input
Some Python scripts depend on gathering input from a user. There are several ways to do this. One popular technique is to use the console to prompt a user for input.
There are two relatively common situations:
- Ordinary input: We can use the
input()
function for this. This will provide a helpful echo of the characters being entered. - Secure, no echo input: This is often used for passwords. The characters entered aren't displayed, providing a degree of privacy. We use the
getpass()
function in thegetpass
module for this.
The input()
and getpass()
functions are just two implementation choices for reading from the console. It turns out that getting the string of characters is only the first step in gathering valid, useful data. The input also needs to be validated.
When gathering input from a user there are several tiers of considerations for us to make, including the following:
- The user interaction...