A Simple and Customizable Password Generator in Python

muhammadabir

Muhammad ABir

Posted on February 7, 2023

A Simple and Customizable Password Generator in Python

In today's digital age, it's important to have strong and secure passwords to protect your personal and sensitive information. However, coming up with new and unique passwords can be difficult and time-consuming. That's where a password generator comes in handy. In this article, we will be building a simple yet customizable password generator using Python.

The Code

We'll start by defining the password_generator function, which takes five parameters: password_length, use_lowercase, use_uppercase, use_digits, and use_symbols. The password_length parameter determines the length of the generated password. The other parameters determine which character sets to include in the password. If a parameter is set to True, the corresponding character set will be included in the password. If a parameter is set to False, it will be excluded.

import random
import string

def password_generator(password_length, use_lowercase, use_uppercase, use_digits, use_symbols):
    # Define the character sets for the password based on the input
    characters = ""
    if use_lowercase:
        characters += string.ascii_lowercase
    if use_uppercase:
        characters += string.ascii_uppercase
    if use_digits:
        characters += string.digits
    if use_symbols:
        characters += "!@#$%^&*()_+=-"

    # Use random.choices() to generate a password of the desired length
    password = "".join(random.choices(characters, k=password_length))
    return password
Enter fullscreen mode Exit fullscreen mode

The function uses the string module's ascii_lowercase and ascii_uppercase constants to define the lowercase and uppercase characters, respectively. The string module's digits constant defines the digit characters. The symbols are defined as a string of commonly used symbols.

The random.choices() function is used to generate a password of the desired length. The function generates a list of random characters from the characters string and then joins the list into a single string using the join() method.

User Input

Next, we'll prompt the user for the desired password length and the character sets to include in the password.

# Prompt the user for the desired password length
password_length = int(input("Enter desired password length: "))

# Prompt the user for the character sets to include in the password
use_lowercase = input("Include lowercase characters? (y/n): ").lower() == "y"
use_uppercase = input("Include uppercase characters? (y/n): ").lower() == "y"
use_digits = input("Include digits? (y/n): ").lower() == "y"
use_symbols = input("Include symbols? (y/n): ").lower() == "y"
Enter fullscreen mode Exit fullscreen mode

The user inputs are converted to boolean values using the comparison operator == "y". If the user enters "y", the corresponding parameter will be set to True, otherwise it will be set to False.

Output

~ python password_generator.py

Enter desired password length: 25
Include lowercase characters? (y/n): n
Include uppercase characters? (y/n): y
Include digits? (y/n): y
Include symbols? (y/n): y
Generated password: 6P&Y1FU(^5_6RL5!$LWKJ4#+W
Enter fullscreen mode Exit fullscreen mode

Here is an web version of this view

💖 💪 🙅 🚩
muhammadabir
Muhammad ABir

Posted on February 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related