Python Password Generator
Table of Contents
In the last lesson, Python Student Management System, we built a complete record-management project. Now we will build a password generator that creates strong random passwords.
In this tutorial, we will build a Python Password Generator from scratch using VS Code. This is a fun and practical beginner project that teaches Python modules such as random and string, loops, functions, conditionals, and user input. By the end, we will have a working tool that generates strong, random passwords based on user preferences.
What Is a Password Generator?
A Password Generator is a program that creates random passwords for us. We tell it how long the password should be. We tell it which types of characters to include — uppercase letters, lowercase letters, digits, and symbols. And the program creates a random password that meets our requirements.
Think of it like a machine that creates keys. Each key is unique and strong. We do not have to think of a password ourselves. The machine does it for us.
Password generators are important because weak passwords are easy to guess. Strong passwords protect our accounts. And a generator can create passwords that are much stronger than anything we could think of.
Features of Our Python Password Generator
Our Password Generator will have these features:
- Ask for password length
- Ask which character types to include
- Generate a random password
- Check password strength
- Show the result
Project Folder Structure
We will create a folder for our project. Inside it, we will have one Python file.
password_generator/ generator.pyThere is no data file for this project. The program just generates passwords on demand.
Let us set up the project in VS Code.
Setting Up the Project in VS Code
Step 1: Open VS Code
If we do not have VS Code installed, we can download it from code.visualstudio.com. It is free.
Once installed, open VS Code.
Step 2: Create a New Folder
- Click on File in the top menu.
- Click Open Folder.
- Navigate to where we want to keep our project (like Desktop or Documents).
- Click New Folder.
- Name it
password_generator. - Click Select Folder.
Now VS Code is open with our new empty folder.
Step 3: Create the Python File
- In the left sidebar, we see our folder name
password_generator. - Right-click on the folder name.
- Click New File.
- Name it
generator.py. - Press Enter.
Now we have an empty generator.py file ready for code.
Step 4: Open the Integrated Terminal
- Click on Terminal in the top menu.
- Click New Terminal.
A terminal panel opens at the bottom. It is already inside our password_generator folder. So we do not need to cd anywhere.
Now we are ready to write code.
How to Create the Password Generator in Python (Step by Step)
We will write the code in small steps. Before each step, we will explain what we are doing and why. We will mention which file we are editing (always generator.py in this project).
Step 1: Import Required Modules
We need two modules. random for random choices. And string for ready-made character sets.
import randomimport stringStep 2: Understand the Character Sets
The string module has some useful character-set constants that we will use.
string.ascii_lowercase— all lowercase letters:abcdefghijklmnopqrstuvwxyzstring.ascii_uppercase— all uppercase letters:ABCDEFGHIJKLMNOPQRSTUVWXYZstring.digits— all digits:0123456789string.punctuation— all symbols:!@#$%^&*()...
We will use these to build our character pool.
Step 3: Create a Function to Generate the Password
We build a pool of characters based on the user’s choices. Then we randomly pick characters from that pool.
def generate_password(length, use_upper, use_lower, use_digits, use_symbols): characters = "" if use_upper: characters += string.ascii_uppercase if use_lower: characters += string.ascii_lowercase if use_digits: characters += string.digits if use_symbols: characters += string.punctuation
if not characters: return None
password = "".join(random.choice(characters) for _ in range(length)) return passwordStep 4: Create a Function to Check Password Strength
We add a point for each good feature. Then we classify the password as Weak, Medium, or Strong.
def check_strength(password): score = 0 if len(password) >= 8: score += 1 if len(password) >= 12: score += 1 if any(c.isupper() for c in password): score += 1 if any(c.islower() for c in password): score += 1 if any(c.isdigit() for c in password): score += 1 if any(c in string.punctuation for c in password): score += 1
if score <= 2: return "Weak" elif score <= 4: return "Medium" else: return "Strong"Step 5: Create the Main Function
We ask the user for the password length and which character types to include. Then we generate the password and show it.
def main(): print("===== PASSWORD GENERATOR =====") try: length = int(input("Enter password length: ")) except ValueError: print("Please enter a valid number") return
use_upper = input("Include uppercase letters? (y/n): ").lower() == "y" use_lower = input("Include lowercase letters? (y/n): ").lower() == "y" use_digits = input("Include digits? (y/n): ").lower() == "y" use_symbols = input("Include symbols? (y/n): ").lower() == "y"
password = generate_password(length, use_upper, use_lower, use_digits, use_symbols) if password: print(f"\nGenerated Password: {password}") print(f"Strength: {check_strength(password)}") else: print("No character types selected. Cannot generate password.")Step 6: Run the Password Generator Program
Finally, let us add the program entry point.
if __name__ == "__main__": main()Complete Python Password Generator Code
Now our complete generator.py file looks like this.
import randomimport string
def generate_password(length, use_upper, use_lower, use_digits, use_symbols): characters = "" if use_upper: characters += string.ascii_uppercase if use_lower: characters += string.ascii_lowercase if use_digits: characters += string.digits if use_symbols: characters += string.punctuation
if not characters: return None
password = "".join(random.choice(characters) for _ in range(length)) return password
def check_strength(password): score = 0 if len(password) >= 8: score += 1 if len(password) >= 12: score += 1 if any(c.isupper() for c in password): score += 1 if any(c.islower() for c in password): score += 1 if any(c.isdigit() for c in password): score += 1 if any(c in string.punctuation for c in password): score += 1
if score <= 2: return "Weak" elif score <= 4: return "Medium" else: return "Strong"
def main(): print("===== PASSWORD GENERATOR =====") try: length = int(input("Enter password length: ")) except ValueError: print("Please enter a valid number") return
use_upper = input("Include uppercase letters? (y/n): ").lower() == "y" use_lower = input("Include lowercase letters? (y/n): ").lower() == "y" use_digits = input("Include digits? (y/n): ").lower() == "y" use_symbols = input("Include symbols? (y/n): ").lower() == "y"
password = generate_password(length, use_upper, use_lower, use_digits, use_symbols) if password: print(f"\nGenerated Password: {password}") print(f"Strength: {check_strength(password)}") else: print("No character types selected. Cannot generate password.")
if __name__ == "__main__": main()How to Run the Password Generator in VS Code
We have two ways to run the program.
Way 1: Using the Terminal
In the VS Code terminal, type:
python generator.pyWay 2: Using the Run Button
- Open
generator.pyin the editor. - Look at the top right corner. There is a small play button (triangle).
- Click it. VS Code will run the file in the terminal.
Both ways work. Use whichever we prefer.
Example Output
===== PASSWORD GENERATOR =====Enter password length: 16Include uppercase letters? (y/n): yInclude lowercase letters? (y/n): yInclude digits? (y/n): yInclude symbols? (y/n): y
Generated Password: k#9Lm!2pQ@8vR$4xStrength: StrongThat is our Password Generator. It is simple but complete. And we built it entirely in VS Code.
Exercises for Practice
Try these exercises to improve the Password Generator.
- Save generated passwords to a file with a timestamp.
- Generate multiple passwords at once.
- Add a passphrase generator that uses random words instead of characters.
- Add an option to exclude similar-looking characters like
l,1,O,0. - Add a copy-to-clipboard feature using the
pyperclipmodule. - Add a strength meter that shows a progress bar.
- Add a menu that lets the user generate another password without restarting the program.
Now let us build the next project: the File Organizer Script. We will also use VS Code for that.
Next, we will build a Python File Organizer Script that automatically sorts files into folders.