PYTHON PROJECT 06

Password Generator

Learn how to generate random passwords using Python strings, lists, random selection, loops and user input.

1. Project Introduction

A Password Generator creates random passwords using different types of characters such as letters, numbers and symbols.

This project is useful for learning how Python can work with strings and random values.

The final project is an educational password generator. For real security systems, use a cryptographically secure random generator and appropriate password management practices.

2. Importing the Random Module

Python provides the random module for generating random values.

import random

3. Creating Character Sets

We can create strings containing the characters that may appear in our password.

lowercase = "abcdefghijklmnopqrstuvwxyz"

uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

numbers = "0123456789"

symbols = "!@#$%^&*"

4. Combining Characters

We can combine all character sets into one string.

characters = (
    lowercase +
    uppercase +
    numbers +
    symbols
)

5. Choosing Password Length

Ask the user how long the password should be.

length = int(
    input("Enter password length: ")
)

6. Creating an Empty Password

Start with an empty string.

password = ""

7. Using a Loop

The loop runs once for every character that needs to be generated.

for i in range(length):

    # generate one character

8. Selecting a Random Character

The random.choice() function selects one random item from a sequence.

character = random.choice(characters)

9. Building the Password

Add the selected character to the password.

password += character

The complete loop becomes:

password = ""

for i in range(length):

    character = random.choice(characters)

    password += character

10. Displaying the Password

print("Generated Password:", password)

11. Validating Password Length

We should prevent invalid or impractical lengths.

if length < 4:

    print("Password should be at least 4 characters.")

We can also define a reasonable maximum length for the application.

12. Using a Function

A function makes the generator reusable.

def generate_password(length):

    characters = (
        lowercase +
        uppercase +
        numbers +
        symbols
    )

    password = ""

    for i in range(length):

        password += random.choice(characters)

    return password

13. Calling the Function

length = int(
    input("Enter password length: ")
)

password = generate_password(length)

print("Generated Password:", password)

14. Adding Error Handling

The user might enter text instead of a number. We can use try / except.

try:

    length = int(
        input("Enter password length: ")
    )

except ValueError:

    print("Please enter a valid number.")

15. Complete Project Flow

01 Import Random
02 Create Characters
03 Choose Length
04 Validate Length
05 Create Empty String
06 Start Loop
07 Pick Character
08 Build Password
09 Return Password
10 Display Result

16. Concepts You Practiced

17. Run Complete Project 🚀

You have learned how a password generator works. Now open the complete working project.

Run Complete Project