Python Random Password List Generator Example

This example shows how to build a simple Python script that generates a list of random passwords.

You will use:

  • the random module
  • strings of allowed characters
  • loops
  • a little user input for customization

This is a good beginner project because it combines several basic Python ideas in one small program.

Quick example

import random
import string

characters = string.ascii_letters + string.digits + string.punctuation

for _ in range(5):
    password = ''.join(random.choice(characters) for _ in range(12))
    print(password)

This quick example prints 5 random passwords, each 12 characters long.

What this example builds

This script creates:

  • a program that generates multiple random passwords
  • passwords made from letters, digits, and symbols
  • an easy way to change password length and how many passwords to create

What you need to understand first

Before looking at the full code, these ideas help:

  • A string can hold all characters that are allowed in the password.
  • random.choice() picks one item from a sequence at random.
  • A loop can repeat the character-picking step to build one full password.
  • Another loop can repeat the whole process to generate many passwords.

Basic password list generator

Here is a simple version:

import random
import string

characters = string.ascii_letters + string.digits + string.punctuation

for _ in range(5):
    password = ''.join(random.choice(characters) for _ in range(12))
    print(password)

Example output

Your output will be different each time, but it may look like this:

d8@Kp!2Lm#Q1
7&nZ4^xP0!aB
m$T9q@W2e*R5
!L8v#Y1c%N6s
Q2@fG7&kP!z4

What this code does

  • import random loads Python's random tools.
  • import string gives you ready-made character groups.
  • string.ascii_letters adds lowercase and uppercase letters.
  • string.digits adds numbers from 0 to 9.
  • string.punctuation adds symbols like !, @, and #.
  • The for _ in range(5) loop creates 5 passwords.
  • The inner part builds a 12-character password by picking one random character at a time.
  • ''.join() combines those characters into one string.

How the code works

This line creates the allowed character set:

characters = string.ascii_letters + string.digits + string.punctuation

It combines:

  • letters
  • digits
  • punctuation symbols

This line builds one password:

password = ''.join(random.choice(characters) for _ in range(12))

Here is what happens:

  • range(12) means "do this 12 times"
  • random.choice(characters) picks one random character each time
  • the generator expression produces 12 random characters
  • join() combines them into one password string

If you want a deeper explanation of random tools, see the Python random module overview.

Let the user control length and count

You can improve the script by asking the user for:

  • password length
  • number of passwords to generate
import random
import string

characters = string.ascii_letters + string.digits + string.punctuation

length = int(input("Enter password length: "))
count = int(input("How many passwords do you want to generate? "))

for _ in range(count):
    password = ''.join(random.choice(characters) for _ in range(length))
    print(password)

How this version works

  • input() reads text typed by the user
  • int() converts that text to an integer
  • length controls how many characters are in each password
  • count controls how many passwords are generated

If you are new to this, read how to get user input in Python and how to convert a string to an int in Python.

Important note about errors

If the user types something that is not a whole number, int() will fail with a ValueError.

For example:

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

If the user enters abc, Python raises an error.

See how to fix ValueError: invalid literal for int() with base 10 if that happens.

Simple improvements

Here are a few easy ways to change the program.

Avoid hard-to-read characters

Some characters are easy to confuse, such as:

  • O and 0
  • l and 1

You can build your own character set:

import random

characters = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"

for _ in range(5):
    password = ''.join(random.choice(characters) for _ in range(12))
    print(password)

Generate passwords without symbols

If you only want letters and numbers:

import random
import string

characters = string.ascii_letters + string.digits

for _ in range(5):
    password = ''.join(random.choice(characters) for _ in range(12))
    print(password)

Store passwords in a list

Instead of printing passwords right away, you can save them in a list:

import random
import string

characters = string.ascii_letters + string.digits + string.punctuation
passwords = []

for _ in range(5):
    password = ''.join(random.choice(characters) for _ in range(12))
    passwords.append(password)

print(passwords)

Write passwords to a text file

You can also save the generated passwords to a file:

import random
import string

characters = string.ascii_letters + string.digits + string.punctuation

with open("passwords.txt", "w") as file:
    for _ in range(5):
        password = ''.join(random.choice(characters) for _ in range(12))
        file.write(password + "\n")

This creates a file named passwords.txt and writes one password per line.

Beginner mistakes to watch for

Common problems in this example include:

  • forgetting to import random or string
  • using input() values as strings instead of integers
  • using random.sample() when repeated characters should be allowed
  • expecting random.choice() to create security-grade passwords

Common errors

You may run into:

  • NameError because random or string was not imported
  • ValueError when converting user input with int()
  • TypeError from using a string number directly in range()
  • confusion about why some passwords contain repeated characters

Useful debugging checks

If something is not working, print values to inspect them:

print(characters)
print(len(characters))
print(password)
print(type(length))
print(type(count))

These checks help you confirm:

  • what characters are available
  • how many characters are in the set
  • what the generated password looks like
  • whether length and count are really integers

When not to use this approach

This example is good for learning, but it is not the best choice for real security.

Keep these points in mind:

  • Do not use the random module for high-security passwords.
  • For stronger password generation, use the secrets module.
  • This example is mainly for practicing loops, strings, and randomness.

If you want a simpler version that focuses on creating one password, see Python password generator example.

FAQ

Why do some generated passwords repeat characters?

Because random.choice() can pick the same character more than once. That is normal for this approach.

Can I generate passwords without symbols?

Yes. Use only string.ascii_letters and string.digits in the character set.

Is the random module safe for real password security?

No. It is fine for learning, but real password tools should use the secrets module.

How do I save the passwords to a file?

Store them in a list or write each one with a file opened in write mode.

See also

Try improving this program next by:

  • taking user input safely
  • saving results to a file
  • rebuilding it with the secrets module for stronger password generation