Skip to content

Random Password Generator

Abstract

Random Password Generator is a simple yet powerful Python application that creates strong, random passwords to help users maintain better security practices. The application generates passwords using a combination of uppercase letters, lowercase letters, numbers, and special characters. Users can specify the desired password length, making it flexible for different security requirements. This project demonstrates working with Python’s randomrandom and stringstring modules, user input handling, and string manipulation techniques.

Prerequisites

  • Python 3.6 or above
  • A code editor or IDE

Before you Start

Before starting this project, you must have Python installed on your computer. If you don’t have Python installed, you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed, you can download Visual Studio Code from here.

Note: This project uses only built-in Python modules (randomrandom and stringstring), so no additional installations are required.

Getting Started

Create a Project

  1. Create a folder named random-password-generatorrandom-password-generator.
  2. Open the folder in your favorite code editor or IDE.
  3. Create a file named randompasswordgenerator.pyrandompasswordgenerator.py.
  4. Copy the given code and paste it in your randompasswordgenerator.pyrandompasswordgenerator.py file.

Write the Code

  1. Copy and paste the following code in your randompasswordgenerator.pyrandompasswordgenerator.py file.
⚙️ Random Password Generator
Random Password Generator
# Random Password Generator
 
import random
import string
 
def randompasswordgenerator():
    print("Random Password Generator")
    print("Enter the length of password: ")
    length = int(input())
    password = ''
    for i in range(length):
        password += random.choice(string.ascii_letters + string.digits + string.punctuation)
    print(password)
    
if __name__ == "__main__":
    randompasswordgenerator() 
Random Password Generator
# Random Password Generator
 
import random
import string
 
def randompasswordgenerator():
    print("Random Password Generator")
    print("Enter the length of password: ")
    length = int(input())
    password = ''
    for i in range(length):
        password += random.choice(string.ascii_letters + string.digits + string.punctuation)
    print(password)
    
if __name__ == "__main__":
    randompasswordgenerator() 
  1. Save the file.
  2. Open the terminal in your code editor or IDE and navigate to the folder random-password-generatorrandom-password-generator.
command
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
12
K8@mN2$pX9!z
 
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
16
aB3#dE7&hI9@kL2$
 
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
8
mK9@nP2!
command
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
12
K8@mN2$pX9!z
 
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
16
aB3#dE7&hI9@kL2$
 
C:\Users\Your Name\random-password-generator> python randompasswordgenerator.py
Random Password Generator
Enter the length of password: 
8
mK9@nP2!

Explanation

  1. Import the required modules.
randompasswordgenerator.py
import random
import string
randompasswordgenerator.py
import random
import string
  1. Define the main password generation function.
randompasswordgenerator.py
def randompasswordgenerator():
    print("Random Password Generator")
    print("Enter the length of password: ")
randompasswordgenerator.py
def randompasswordgenerator():
    print("Random Password Generator")
    print("Enter the length of password: ")
  1. Get user input for password length.
randompasswordgenerator.py
length = int(input())
password = ''
randompasswordgenerator.py
length = int(input())
password = ''
  1. Generate the password using random characters.
randompasswordgenerator.py
for i in range(length):
    password += random.choice(string.ascii_letters + string.digits + string.punctuation)
print(password)
randompasswordgenerator.py
for i in range(length):
    password += random.choice(string.ascii_letters + string.digits + string.punctuation)
print(password)
  1. Execute the function when script is run directly.
randompasswordgenerator.py
if __name__ == "__main__":
    randompasswordgenerator()
randompasswordgenerator.py
if __name__ == "__main__":
    randompasswordgenerator()

Character Sets Used

The password generator uses the following character sets from Python’s stringstring module:

  • string.ascii_lettersstring.ascii_letters: Both uppercase and lowercase letters (a-z, A-Z)
  • string.digitsstring.digits: Numbers (0-9)
  • string.punctuationstring.punctuation: Special characters and symbols

This combination ensures strong passwords with high entropy and complexity.

Features

  • Customizable Length: Users can specify any password length
  • Strong Character Mix: Includes letters, numbers, and special characters
  • High Randomness: Uses Python’s secure random module
  • Simple Interface: Easy-to-use command-line interface
  • Instant Generation: Fast password creation
  • No External Dependencies: Uses only built-in Python modules

Security Considerations

  • Character Variety: Uses all types of characters for maximum security
  • Randomness: Each character is independently randomly selected
  • Length Flexibility: Supports any length for different security requirements
  • No Patterns: Generated passwords have no predictable patterns

Password Strength Guidelines

  • Minimum 8 characters: For basic security
  • 12+ characters: For good security
  • 16+ characters: For excellent security
  • Include all character types: Letters, numbers, and symbols

Next Steps

You can enhance this project by:

  • Adding a GUI interface using Tkinter
  • Implementing password strength indicators
  • Adding options to exclude similar characters (0, O, l, 1)
  • Creating memorable password options (using words + numbers)
  • Adding password history (with secure storage)
  • Implementing copy-to-clipboard functionality
  • Adding batch password generation
  • Creating password validation against common passwords
  • Adding custom character set options
  • Implementing pronunciation guides for generated passwords

Enhanced Version Ideas

randompasswordgenerator.py
def enhanced_password_generator():
    # Add options for:
    # - Exclude ambiguous characters
    # - Ensure at least one of each character type
    # - Generate multiple passwords at once
    # - Save passwords to encrypted file
    pass
randompasswordgenerator.py
def enhanced_password_generator():
    # Add options for:
    # - Exclude ambiguous characters
    # - Ensure at least one of each character type
    # - Generate multiple passwords at once
    # - Save passwords to encrypted file
    pass

Common Use Cases

  • User Account Creation: Generate strong passwords for new accounts
  • Password Updates: Create new passwords for existing accounts
  • Temporary Passwords: Generate one-time use passwords
  • API Keys: Create random strings for API authentication
  • Security Testing: Generate test passwords for applications

Best Practices

  • Never Reuse: Generate a unique password for each account
  • Store Securely: Use a password manager to store generated passwords
  • Regular Updates: Change passwords periodically
  • Avoid Predictable Patterns: Use random generation over patterns
  • Backup Safely: Keep secure backups of important passwords

Conclusion

In this project, we learned how to create a Random Password Generator using Python’s built-in modules. We explored the randomrandom module for generating random selections and the stringstring module for accessing different character sets. This project demonstrates essential security programming concepts and provides a practical tool for everyday use. The simplicity of the implementation makes it an excellent starting point for more advanced security-related projects. To find more projects like this, you can visit Python Central Hub.

Was this page helpful?

Let us know how we did