A simple command-line password generator built with Python. This program creates strong, random passwords of a user-defined length, with an option to include symbols, and displays the password's entropy (a measure of how strong it is).
Built as part of a Python fundamentals training track — focused on secure randomness, string manipulation, input validation, and basic cryptographic concepts.
- 🎲 Generates secure random passwords using Python's
secretsmodule - 🔢 User chooses password length (minimum 8 characters)
- 🔣 Option to include symbols for extra strength
- 📊 Displays password entropy in bits
- 🛡️ Validates user input for password length and symbol selection
=== Random Password Generator ===
Enter password length (minimum 8): 12
Include symbols? (y/n): y
Generated Password: aQ8#kzL2!mPr
Entropy: 78.7 bits
python password_generator.pyRequires only Python 3 — no external libraries (secrets, string, and math are all built-in).
import secrets
import string
import math
def generate_password(length, use_symbols):
characters = string.ascii_letters + string.digits
if use_symbols:
characters += string.punctuation
password_list = [secrets.choice(characters) for _ in range(length)]
return "".join(password_list)
def main():
print("=== Random Password Generator ===")
while True:
user_input = input("Enter password length (minimum 8): ")
if user_input.isdigit() and int(user_input) >= 8:
length = int(user_input)
break
print("Please enter a number 8 or greater.")
while True:
choice = input("Include symbols? (y/n): ").lower()
if choice in ("y", "n"):
break
print("Please enter y or n.")
use_symbols = (choice == "y")
password = generate_password(length, use_symbols)
character_count = 94 if use_symbols else 62
entropy = length * math.log2(character_count)
print("Generated Password:", password)
print(f"Entropy: {entropy:.1f} bits")
if __name__ == "__main__":
main()| Concept | Where it's used |
|---|---|
| Secure randomness | secrets.choice() for cryptographically secure password generation |
| String manipulation | Combining string.ascii_letters, string.digits, and string.punctuation |
| List comprehension | [secrets.choice(characters) for _ in range(length)] |
| Input validation | Validating password length and symbol selection using loops |
| Functions | generate_password() and main() |
| Math / entropy calculation | length * math.log2(character_count) |
- Python 3
secrets,string, andmath(standard library)
Tabassum Naseer