Wednesday, December 14, 2022

Random Password Generator using Python

 Hello! Today we will be making a randon password generator using Python.

The program will accept an integer length of password to be generated.

Code:

import random

# Function to generate a password

def generate_password(length):

   password = ""

  # List of characters to choose from

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

  

  # Generate a password of the specified length

  for i in range(length):

    password += random.choice(chars)

    

  # Return the generated password

  return password


# Test the password generator

print(generate_password(8))  # Output: eg. "w4V6Bh8#"

print(generate_password(12)) # Output: eg. "a3!fB5gL7@m9D"

print(generate_password(16)) # Output: eg. "g5J7Fp3#h4V1@m6D8"


This program defines a "generate_password()" function that takes a password length as an input and returns a randomly generated password of that length. The password is generated by choosing random characters from a list of allowed characters. You can modify the list of allowed characters to suit your needs.

No comments:

Post a Comment