Python Projects

Password Generator with Python


Python Password Generator

This page explores how to develop a password generator in Python.

The code requires basic understanding of Python coding as well as the built-in modules of String and Random.

If you are not familiar with Python coding, please check these tutorials first.

Let’s start.

We use Python version 3.10+ and PyCharm (IDE).

Plan

This calculator contains 3 parts:

  1. Import modules
  2. Generation function
  3. Main function

The calculator requests the user to input the length of the password (as integer). As an output, the program produces a randomly generated password.

Import modules

This section is very straightforward.

We need to import the built-in libraries first.

import random
import string

The random module offers character generators and the string module provides string manipulation.

Generation function

The second part covers the function that generates the random password.

First, we define and store in a variable all letters (both lowercase and uppercase), digits (0-9), and punctuation characters. Then, we randomly choose and join (without spaces) characters form the above three packs of constants – all within the range of the chosen password length. And finally, we return the generated password.

def generate_password(length):
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for _ in range(length))
    return password

Main function

The final section is to do with the main function. This function encompasses the required user password length input. We implement a try/except testing, in order to catch any invalid inputs (whether it is a length less than 0 or anything rather than an integer).

def main():
    try:
        length = int(input("Enter the length of the password you want to generate: "))
        if length <= 0:
            print("Please enter a positive integer greater than 0.")
            return
        password = generate_password(length)
        print("Generated Password:", password)
    except ValueError:
        print("Invalid input. Please enter a valid integer.")

Finally, we implement a script execution mechanism.

if __name__ == "__main__":
    main()

Example

Below is an example of the outcome of the password generator.

Full code of Password Generator

Below is the full Python code of the password generator.

import random
import string

def generate_password(length):
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for _ in range(length))
    return password

def main():
    try:
        length = int(input("Enter the length of the password you want to generate: "))
        if length <= 0:
            print("Please enter a positive integer greater than 0.")
            return
        password = generate_password(length)
        print("Generated Password:", password)
    except ValueError:
        print("Invalid input. Please enter a valid integer.")

if __name__ == "__main__":
    main()

Previous: GUI Age Calculator with Python

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.