Lostinspace
Lostinspace
import random
import string
def generate_password(length=12):
uppercase = string.ascii_uppercase
lowercase = string.ascii_lowercase
digits = string.digits
special_characters = string.punctuation
# 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)
random.shuffle(password)
return ''.join(password)
# Example usage
if __name__ == "__main__":
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.