Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Python Project: Password Generator (5 Valuable Programming Skills)

    Implement a password generator

    Implementing a password generator in Python will teach you new skills.

    1. String manipulation. A password generator typically involves manipulating strings in order to create random sequences of characters that meet certain criteria (e.g., length, complexity). As such, a programmer will gain experience with string concatenation, slicing, and other operations that are commonly used in string manipulation.
    2. Random number generation. Generating random numbers is a core component of any password generator. A programmer will learn how to use Python’s built-in random module to generate random integers and other values.
    3. Conditional statements. A password generator will typically need to make decisions based on the user’s input (e.g., if the user specifies a length of 8 characters, the generator should create a password that is exactly 8 characters long). This will require the use of conditional statements (e.g., if, else) to handle different cases.
    4. Loops. A password generator may need to repeat certain operations (e.g., generating random characters) multiple times in order to create a password that meets the desired criteria. This will require the use of loops (e.g., while, for) to iterate over a sequence of characters or numbers.
    5. Secure password generation. Creating a secure password that cannot be easily guessed by an attacker is a key goal of any password generator. A programmer will learn about common password security best practices (e.g., avoiding common words, using a mix of uppercase and lowercase letters, including numbers and symbols) and how to implement them in code.

    Are you ready?

    Project Description

    Create a password generator in Python.

    The password should in this format (with a dash - every 4th character).

    • vr)5-x?C(-8FNh-SkfD

    The password should have:

    • at least one lowercase character: abcdefghijklmnopqrstuvwxyz
    • at least one uppercase character: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    • at least one decimal: 01234567890
    • at least one special character: !@#$%^&*()?

    How do you do that?

    Step 1 Getting the characters

    A simple way to keep track of which characters are needed in the password is to simply have them in each variable.

    This makes the code understandable later.

    lower = 'abcdefghijklmnopqrstuvwxyz'
    upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    decimal = '01234567890'
    special = '!@#$%^&*()?'
    

    Step 2 Checking if the password is strong

    A strong password should have at least one of each of the 4 groups of characters: lower, upper, decimal, and special.

    To not complicate code, a great way is to keep the functionality simple by creating your own simple functions using loops and conditionals.

    def contains(letters, password):
        for letter in letters:
            if letter in password:
                return True
        return False
    
    def strong_password(password):
        if not contains(lower, password):
            return False
        if not contains(upper, password):
            return False
        if not contains(decimal, password):
            return False
        if not contains(special, password):
            return False
        
        return True
    

    The logic is simple. For each group check if the password contains the type of characters. If not, return False. If it passes all tests, then the password must be strong.

    Step 3 Generate strong passwords

    Now to the most difficult part.

    How do we generate a strong password?

    The simple answer is, to generate random passwords until one is strong.

    To do that we need randomness.

    import random
    legal_chars = lower + upper + decimal + special
    while True:
        password = ''
        for _ in range(16):
            password += random.choice(legal_chars)
            
        if strong_password(password):
            password = password[:4] + '-' + password[4:8] + '-' + password[8:12] + '-' + password[12:]
            
            print(password)
            break
    

    You see, keep making them until one succeeds. This way you don’t compromise the randomness in the password generator.

    Want more Python projects?

    This is part of 19 Python Projects and you can create an anagram game and get 5 core programming skills.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment