Build a Secure Password Generator with Python (Beginner-Friendly Project)

A simple Python project that helps you build strong, customizable passwords with just a few lines of code.
As a beginner learning Python, I wanted to work on something simple yet practical. We use passwords everywhere โ and creating secure ones manually can be tough. So I built a password generator that takes user preferences like length, symbols, digits, and uppercase letters โ and generates a strong password in seconds.
This was a great learning experience and took less than an hour to build!
๐ ๏ธ Technologies Used:
Python 3
Built-in random and string libraries
๐งพ Features:
โ
Ask user for desired password length
โ
Optionally include:
โโ๐น Uppercase letters
โโ๐น Digits
โโ๐น Special characters (@, #, $, etc.)
โ
Ensure at least one character from each selected category
โ
Shuffle characters for randomness
โ
Prevent weak (short) passwords
๐ก Full Python Code:
import random
import string
def generate_password():
while True:
try:
length=input("Enter the desired password length: ")
if int(length)<4:
print("Password length should be at least 4 characters.")
continue
else:
break
except ValueError:
print("Please enter a valid number")
continue
include_uppercase=input("Do you want to include uppercase (Yes/No)? ").strip().lower()
include_symbol=input("Do you want to include special characters (Yes/No)? ").strip().lower()
include_digits=input("Do you want to include digits (Yes/No)? ").strip().lower()
lower=string.ascii_lowercase
uppercase=string.ascii_uppercase if include_uppercase=="yes" else ""
special=string.punctuation if include_symbol=="yes" else ""
digits=string.digits if include_digits=="yes" else ""
all_characters=lower+uppercase+special+digits
required_characters=[]
if include_uppercase=="yes":
required_characters.append(random.choice(uppercase))
if include_symbol=="yes":
required_characters.append(random.choice(special))
if include_digits=="yes":
required_characters.append(random.choice(digits))
remaining_length=int(length)-len(required_characters)
password=required_characters.copy()
for _ in range(remaining_length):
character=random.choice(all_characters)
password.append(character)
random.shuffle(password)
str_password="".join(password)
return str_password
print(generate_password())
๐ฅ๏ธ Sample Output:
Enter the desired password length: 8
Do you want to include uppercase (Yes/No)? yes
Do you want to include special characters (Yes/No)? yes
Do you want to include digits (Yes/No)? yes
0(2#20Sz
๐ What I Learned
Handling user input
Using built-in modules like random and string
Ensuring security logic in code (minimum character requirements)
How to think about edge cases
โจ Final Thoughts
If youโre new to Python, building mini projects like this is a great way to apply what you learn quickly. Not only is it fun, but itโs also genuinely useful!
Iโd love to hear your thoughts or see your own versions of this project โ feel free to share your ideas or improvements in the comments below!
Subscribe to my newsletter
Read articles from Harsimran Kaur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harsimran Kaur
Harsimran Kaur
Computer Science student passionate about programming and Python. Exploring algorithms, problem-solving, and machine learning through projects and coding challenges. Sharing my learning journey and helpful tutorials to grow with the developer community.