Learnmonkey Learnmonkey Logo

Password Generator

In this tutorial, we'll create a password generator in Python that prompts the user for details such as the length of the password, the number of digits, the number of letters, etc. The password generator will run in the command line, so we are making a command line interface (CLI) program.

The string Module

In order to generate a random pasword, we'll need to allow the user to specify how many letters or digits or special characters to put in the password. To do so, we'll need to have the letters/digits/characters on hand for us to access. We can do this by using the string module that comes built-in with your default Python installation. We can use the string module by importing it.

import string

Using The string Module

We already know what the string module is, so now let's start using it.

Generating Random Letters

The string module ships with a lot of useful variables. One of them is ascii_letters, which is basically a string of all the uppercase and lowercase characters. We can access this by typing string.ascii_letters.

Extensions

Fun extension ideas: