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 the user for input.
There are two relatively common situations:
- Ordinary input: We use the
input()
function for this. This will provide a helpful echo of the characters being entered. - 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 processing. We actually have separate tiers of considerations:
- The initial interaction with the console. This is the basics of writing a prompt and reading input. This must correctly handle data as well as keyboard events, such as backspace for editing. This may also mean...