DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Building a Password Generator in Python

πŸ“Œ Quick Info

  • Topic: A beginner project using Python's built-in libraries
  • Target Audience: Beginners who want a second project
  • Goal: Build something useful in under 40 lines

1. Introduction

"The first project I built was a weather app. The second was a password generator. It took 30 minutes and I use it almost every week."


2. What We're Building

A CLI tool that generates a strong random password:

python password.py
Enter fullscreen mode Exit fullscreen mode

Output:

Your password: 7Kx!m2Qa#9Lp
Enter fullscreen mode Exit fullscreen mode

You can also choose the length and whether to include symbols.


3. Why This Is a Good Beginner Project

  • Uses built-in Python Libraries (no pip install needed)
  • Teaches modules, strings, loops, and user input
  • You end up with something you’ll actually use
  • Small enough to finish in one sitting

4. The Code

import random
import string

def generate_password(length=12, use_symbols=True):
    letters = string.ascii_letters + string.digits
    symbols = "!@#$%^&*()-_=+"

    characters = letters
    if use_symbols:
        characters += symbols

    password = "".join(random.choice(characters) for _ in range(length))
    return password

def main():
    try:
        length = int(input("Password length (default 12): ") or 12)
    except ValueError:
        print("Invalid length. Using 12.")
        length = 12

    use_symbols = input("Include symbols? (y/n): ").lower() != "n"

    password = generate_password(length, use_symbols)
    print(f"\nYour password: {password}")

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

5. How It Works (Line By Line)

  • Line 1-2: Import random (for picking characters) and string (for ready-made character sets).
  • Line 4: generate_password() takes a length and a flag for symbols.
  • Line 5-6: Build the character pool- letters, digits, and optional symbols.
  • Line 9: β€œβ€. join(random.choice(characters)for_in range(length)) picks a random character length times and joins them into one string.
  • Line 12-22: main() handles user input and calls the generator.

6. Real Example (What It Looks Like In Action)

Password length (default 12): 16
Include symbols? (y/n): y

Your password: 7Kx!m2Qa#9LpWn$3f
Enter fullscreen mode Exit fullscreen mode

Strong, random, and 16 characters long. That’s a better password than most people use.


7. What I learned Building This

  1. random.choice() is the core of randomness in Python
  2. string module has ready-made character sets (ascii_letters, digits, punctuation)
  3. "".join(...) is how you turn a list of characters into a string
  4. input() returns a string β€” you have to convert it with int() if you need a number
  5. try/except keeps the program from crashing on bad input

8. What To Add Next

  • Save generated passwords to a file (encrypted, ideally)
  • Copy to clipboard using pyperclip
  • Add a strength checker that scores the password
  • Generate passphrases instead of random strings (e.g., "correct-horse-battery-staple")
  • Turn it into a GUI with Tkinter

9. Conclusion

"This project took 30 minutes to build and I've used it dozens of times since. That's the thing about small projects β€” they teach you the syntax and they earn their place in your workflow. Build something small. Use it. Then build the next one.”

Top comments (0)