0
0
PythonProgramBeginner · 2 min read

Python Program to Generate Password with Random Characters

Use the random and string modules to generate a password by combining letters, digits, and symbols, for example: import random, string; password = ''.join(random.choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(12)).
📋

Examples

Inputlength = 8
OutputGenerated password: aB3$dE1!
Inputlength = 12
OutputGenerated password: G7#kLm9!pQr2
Inputlength = 4
OutputGenerated password: 4f@Z
🧠

How to Think About It

To create a password, first decide how long it should be. Then pick random characters from a mix of uppercase letters, lowercase letters, numbers, and symbols. Combine these random picks into a string to form the password.
📐

Algorithm

1
Import modules for random choice and character sets.
2
Define the length of the password.
3
Create a string containing all allowed characters (letters, digits, symbols).
4
Randomly select characters from this string until reaching the desired length.
5
Join the selected characters into one string.
6
Return or print the generated password.
💻

Code

python
import random
import string

def generate_password(length=12):
    chars = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(chars) for _ in range(length))
    return password

print("Generated password:", generate_password(12))
Output
Generated password: (random 12-character string with letters, digits, symbols)
🔍

Dry Run

Let's trace generating a password of length 4 through the code

1

Import modules

random and string modules are ready to use

2

Set length

length = 4

3

Create character set

chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

4

Select characters

random picks: ['G', '7', '#', 'k']

5

Join characters

password = 'G7#k'

6

Return password

Output: 'G7#k'

IterationSelected Character
1G
27
3#
4k
💡

Why This Works

Step 1: Importing modules

We use random to pick characters randomly and string to get sets of letters, digits, and symbols.

Step 2: Combining characters

All allowed characters are combined into one string so we can pick from them easily.

Step 3: Generating password

We pick random characters one by one until we reach the desired length, then join them into the final password string.

🔄

Alternative Approaches

Using secrets module for better security
python
import secrets
import string

def generate_secure_password(length=12):
    chars = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(secrets.choice(chars) for _ in range(length))
    return password

print("Secure password:", generate_secure_password(12))
The secrets module is better for security-sensitive password generation but slightly slower.
Using only letters and digits
python
import random
import string

def generate_simple_password(length=12):
    chars = string.ascii_letters + string.digits
    password = ''.join(random.choice(chars) for _ in range(length))
    return password

print("Simple password:", generate_simple_password(12))
This method avoids symbols, making passwords easier to type but less complex.

Complexity: O(n) time, O(n) space

Time Complexity

The program runs in O(n) time because it selects one character at a time for the password length n.

Space Complexity

It uses O(n) space to store the generated password string of length n.

Which Approach is Fastest?

Using random.choice is fast and simple, but secrets.choice is more secure though slightly slower.

ApproachTimeSpaceBest For
random.choice with letters+digits+symbolsO(n)O(n)General use, fast
secrets.choice with letters+digits+symbolsO(n)O(n)Security-sensitive password generation
random.choice with letters+digits onlyO(n)O(n)Simple passwords, easier typing
💡
Use the secrets module instead of random for generating passwords in real security applications.
⚠️
Beginners often forget to include symbols or digits, making passwords weak and easy to guess.