How to create a password generator in Python

In today’s digital age, passwords stand as the first line of defense against unauthorized access to sensitive information. Whether it’s our email, social media, or online banking, a robust and unique password is crucial for maintaining our online security. The significance of a password’s strength can’t be overstated, because weak passwords are often the weakest link in cybersecurity. To enhance our online security, we can create a password generator in Python.

In this Answer, we’ll explore how to build a simple yet effective password generator that will assist us in creating strong and secure passwords.

To truly appreciate the importance of a strong password, let’s consider an example that highlights the difference between weak and strong choices:

  • Weak password: “qwerty”
  • Strong password: “P@$$w0rd!2023”

The password “qwerty” is considered weak because it is incredibly common and easily guessable, whereas the strong password “P@$$w0rd!2023” incorporates various elements to enhance security. It includes a mix of uppercase and lowercase letters, numbers, and special characters.

Fundamentals of a strong password

The password generator we’ll build using Python will ensure that these key criteria are met:

  • Adequate length: A longer password is generally more secure. Our generator will create a password with a length of 1010 characters, ensuring it is sufficiently robust to resist brute-force attacks.

  • Inclusion of capital alphabets: Mixing uppercase and lowercase letters increases the complexity of the password.

  • Special character inclusion: Special characters such as !, @, #, $, etc., add a layer of complexity to the password.

  • Incorporation of numbers: Including numbers makes guessing a password more challenging.

Implementation

import random
import string
def generate_password():
# Define character sets
lowercase_letters = string.ascii_lowercase
uppercase_letters = string.ascii_uppercase
digits = string.digits
special_characters = '!@#$%^&*()_+[]{}|;:,.<>?`~'
# Ensure at least one of each type of character
password = [
random.choice(lowercase_letters),
random.choice(uppercase_letters),
random.choice(digits),
random.choice(special_characters)
]
# Fill the rest of the password with random characters
password.extend(random.choice(lowercase_letters + uppercase_letters + digits + special_characters) for _ in range(6))
# Shuffle the characters to make the password more random
random.shuffle(password)
return ''.join(password)
# Driver code
def main():
for i in range(5):
# Generate a password
password = generate_password()
print(str(i + 1) + ") Generated Password:", password)
if __name__ == "__main__":
main()
Password generator

Explanation

Let’s go through the code line by line to understand each part:

  • Lines 1–2: We import the random and string modules to generate random numbers and access a collection of characters, respectively.

  • Lines 6–9: We define four separate sets of characters: lowercase letters, uppercase letters, digits, and special characters. These sets will be used to create the password.

  • Lines 12–17: In this section, we ensure that the password includes at least one character from each defined set (lowercase letters, uppercase letters, digits, and special characters). This is achieved by randomly selecting one character from each set and adding it to the password list.

  • Line 20: Here, a loop is used to fill the remaining characters of the password. We utilize random.choice() to select a random character from the combined set of lowercase letters, uppercase letters, digits, and special characters. This process is repeated 66 times to achieve a total length of 1010 characters.

  • Line 23: To add an extra layer of randomness, characters in the password are shuffled. This ensures that the characters are not in a predictable order.

  • Line 25: Finally, the characters in the password list are joined to create a single string. This resulting string is the generated password, which is then returned by the function.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved