Build a Python Project: Password Generator
One of the good practices is building a project, here we will build a password generator using Python.
In the era of internet, where digital accounts are as common as air. Creating strong and secure passwords is a constant struggle. Lets building project that will create password for you.
Lets plan the project 📔
First before we build it, we need to know what we want from this project which is :
It can generate random number, symbols, and letter.
Here what you will learn while building the project :
- Variables and data types
- User input and output
- Conditional statements (if-else)
- Random number generation
- Functions
Now we get our goals, lets illustrate the flow of our project.
- Get the length of password, in here I put “12” as the default length
- Create character pool (lowercase, uppercase, symbols, numbers)
- Generate Password
Lets Build the Project
1. Import Libraries:
The code starts by importing two necessary libraries: string
and random
.
string
: This library provides various string-related functions and constants. It contains theascii_lowercase
,ascii_uppercase
,punctuation
, anddigits
constants, which represent the respective character sets.random
: This library provides functions for generating random numbers and sequences. It contains thechoice()
function, which randomly selects an element from a given sequence.
import string
import random
2. Define generate_password()
Function:
The generate_password()
function is the core of the password generator. It takes an optional parameter length
that specifies the desired password length.
Default Length:** If no
length` is provided, the function uses a default length of 12 characters.Character Pool:** The function creates a character pool by concatenating the lowercase letters (
ascii_lowercase), uppercase letters (
ascii_uppercase), symbols (
punctuation), and digits (
digits) from the
string` library.Password Generation:** The function initializes an empty
passwordstring. Then, it iterates
lengthtimes, randomly selecting a character from the character pool and adding it to the
password` string.
def generate_password(length=12):
# Define the character pool
character_pool = string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.digits
password = ""
for _ in range(length):
password += random.choice(character_pool)
return password
3. Generate and Display Password:
Outside the function, the code calls the generate_password()
function to generate a random password. It assigns the generated password to the generated_password
variable and displays it to the user using the print()
function.
# Generate and display the password
generated_password = generate_password()
print("Your generated password is:", generated_password)
How about we elevate the password generator with some features :
- User can input the length of password
- User have option to choose if they want to use symbols, numbers, and uppercase.
Alright lets add new code in our project;
1. Define Character Sets:
lowercase_letters = string.ascii_lowercase
uppercase_letters = string.ascii_uppercase
symbols = string.punctuation
numbers = string.digits
These lines of code define four variables: lowercase_letters
, uppercase_letters
, symbols
, and numbers
. Each variable holds a string containing the corresponding character set. The string
module provides these constants for easy access to different character groups.
2. Create the character pool based on user-specified criteria
character_pool = lowercase_letters
if include_symbols:
character_pool += symbols
if include_numbers:
character_pool += numbers
if include_uppercase:
character_pool += uppercase_letters
These if
statements check the user's input for each character type (symbols, numbers, uppercase). If the user indicates that they want to include a particular character type, the corresponding character set is appended to the character_pool
string. This ensures that the password generator considers the specified character types when generating passwords.
3. Generated Password
# Generate the password
for _ in range(length):
password += random.choice(character_pool)
return password
This loop iterates length
times, where length
is the specified password length. In each iteration, a random character is selected from the character_pool
using the random.choice()
function. The selected character is then appended to the password
string. lastly it will return the password string.
4. Get user input for password criteria
# Get user input for password criteria
password_length = int(input("Enter desired password length: "))
include_symbols = input("Include symbols (y/n): ") == "y"
include_numbers = input("Include numbers (y/n): ") == "y"
include_uppercase = input("Include uppercase letters (y/n): ") == "y"
these lines gather the user’s preferences regarding the password criteria (length, symbols, numbers, and uppercase letters) and store them in the corresponding variables.
Lets see how it works
Conclusion
The provided password generator is a simple and effective tool for generating passwords. It allows users to customize the password length and character types, making it versatile and adaptable to individual needs.
Hey 👋 Enjoying the content? If you find it valuable, why not subscribe and follow Me on Medium for more content?
🔔 Subscribe & Follow
☕️Buymeacoffee |📚Substack | GitHub | LinkedIn
By subscribing, you’ll get the latest updates, tips, and content delivered right to your inbox.
Thank you for your support! 🙌
In this post, I kindly want to see the effectiveness of Call to Action (CTA).
PlainEnglish.io 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer️
- Learn how you can also write for In Plain English️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: Stackademic | CoFeed | Venture