0% found this document useful (0 votes)
20 views2 pages

Lostinspace

This document provides a Python code snippet for generating a random password that includes uppercase letters, lowercase letters, digits, and special characters. Users can customize the password length and the code ensures that at least one character from each category is included. Instructions for usage are also provided, making it easy to implement in a Python environment.

Uploaded by

Hajra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Lostinspace

This document provides a Python code snippet for generating a random password that includes uppercase letters, lowercase letters, digits, and special characters. Users can customize the password length and the code ensures that at least one character from each category is included. Instructions for usage are also provided, making it easy to implement in a Python environment.

Uploaded by

Hajra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Here’s a simple Python code snippet that generates a random password.

The password will


include uppercase letters, lowercase letters, digits, and special characters. You can customize
the length of the password as needed.

import random

import string

def generate_password(length=12):

# Define the character sets

uppercase = string.ascii_uppercase

lowercase = string.ascii_lowercase

digits = string.digits

special_characters = string.punctuation

# Combine all characters

all_characters = uppercase + lowercase + digits + special_characters

# Ensure the password includes at least one character from each category

password = [

random.choice(uppercase),

random.choice(lowercase),

random.choice(digits),

random.choice(special_characters)

# Fill the rest of the password length

password += random.choices(all_characters, k=length - 4)


# Shuffle the password list to ensure randomness

random.shuffle(password)

# Convert list to string

return ''.join(password)

# Example usage

if __name__ == "__main__":

password_length = 12 # You can change this value

print("Generated Password:", generate_password(password_length))

How to Use

1. Copy the code into a Python environment (like Jupyter Notebook, or any Python IDE).

2. Run the script, and it will generate a random password of the specified length.

3. You can modify the password_length variable to get a password of different lengths.

This code ensures that the generated password is strong and meets common security standards.

You might also like