Code Python
Code Python
import secrets
import string
def generate_password(length):
# Define the characters to use in the password
characters = string.ascii_letters + string.digits + string.punctuation
# Example usage
password_length = 12 # Set the desired length of the password
print("Generated Password:", generate_password(password_length))
1. **Imports**:
- `secrets`: For generating secure random numbers.
- `string`: For pre-defined character sets.
2. **Function `generate_password(length)`**:
- Takes a `length` argument to specify how long the password should be.
- `characters` combines letters, digits, and special characters into one string.
- `secrets.choice(characters)` picks a random character from the combined set.
- `''.join(...)` assembles the chosen characters into a single string.
3. **Main Block**:
- Sets `password_length` to 12 (you can change this to any length you want).
- Prints the generated password.
ANOTHER RANDOM CODE :
import secrets
import string
def generate_password(length=12):
# Define the character sets to choose from
alphabet = string.ascii_letters # a-z and A-Z
digits = string.digits # 0-9
special_characters = string.punctuation # Special characters like !@#$%^&*
# Example usage
if __name__ == "__main__":
password_length = 16 # Set the desired length of the password
print("Generated Password:", generate_password(password_length))