Python Password Generator Project

Python project 06

Python Password Generator Project

Last updated: 31 August 2026

Create a secure command-line password generator with Python’s secrets module. This project teaches random selection, character sets, input validation, and a security-focused user experience.

Join the Python programming courseView all project ideas

What you will build

The user chooses a length of 12 characters or more. The program creates a password with lowercase letters, uppercase letters, numbers, and symbols, then offers to generate another one.

Skills practised

  • Python modules
  • Strings and character sets
  • Lists and shuffling
  • Validation loops

Requirements

  • Python 3.8 or later
  • A terminal or command prompt
  • No external packages
  • About 30 minutes to build

Full Python code

Save the code as password_generator.py, then run it from a terminal.

"""Generate secure passwords with Python's secrets module."""

import secrets
import string


def read_length():
    while True:
        try:
            length = int(input("Password length (12 or more): ").strip())
            if length < 12:
                raise ValueError
            return length
        except ValueError:
            print("Enter a whole number of at least 12.")


def make_password(length):
    """Create a password with upper/lowercase letters, digits, and symbols."""
    symbols = "!@#$%^&*?-_"
    required = [
        secrets.choice(string.ascii_lowercase),
        secrets.choice(string.ascii_uppercase),
        secrets.choice(string.digits),
        secrets.choice(symbols),
    ]
    alphabet = string.ascii_letters + string.digits + symbols
    password = required + [secrets.choice(alphabet) for _ in range(length - len(required))]
    secrets.SystemRandom().shuffle(password)
    return "".join(password)


def main():
    print("Secure Password Generator")
    print("Each password includes uppercase, lowercase, number, and symbol characters.\n")
    while True:
        length = read_length()
        print(f"\nGenerated password: {make_password(length)}\n")
        again = input("Generate another? (y/n): ").strip().lower()
        if again not in {"y", "yes"}:
            print("Keep the password private and store it in a trusted password manager.")
            break


if __name__ == "__main__":
    main()

How it works

make_password() first selects one character from every required group. It fills the remaining positions from the complete character set, then uses a secure shuffle so character positions are not predictable. The secrets module is appropriate for passwords and tokens.

Run the project

  1. Open a terminal in the project folder.
  2. Run python password_generator.py.
  3. Enter a length such as 16.
  4. Store the result only in a trusted password manager.

Notes for your notebook

  • secrets: use it for security-sensitive random values.
  • Strong passwords: prefer unique, long passwords for every account.
  • Never log passwords: do not save or commit generated passwords.

Ways to extend it

Add a passphrase mode, a setting to exclude ambiguous characters, a strength explanation, or a small Tkinter interface. Do not add silent password storage.

What to learn next

Practise Python functions and choose another project from the Softenant project library.