Back to Freecodecamp

Step 27

curriculum/challenges/english/blocks/learn-regular-expressions-by-building-a-password-generator/656494269ccce754411a2112.md

latest1.1 KB
Original Source

--description--

The re module allows you to use regular expressions in your code. You will learn more about regular expressions very soon.

For now, add an import statement at the top of your code to import the re module.

--hints--

You should import the re module.

js
({ test: () => assert.match(code, /^import\s+re/m) })

--seed--

--seed-contents--

py
--fcc-editable-region--

import secrets
import string
--fcc-editable-region--

def generate_password(length, nums, special_chars, uppercase, lowercase):
    # Define the possible characters for the password
    letters = string.ascii_letters
    digits = string.digits
    symbols = string.punctuation

    # Combine all characters
    all_characters = letters + digits + symbols

    while True:
        password = ''
        # Generate password
        for _ in range(length):
            password += secrets.choice(all_characters)
        
        constraints = [
            (nums, '')
        ]        

    return password

# new_password = generate_password(8)
# print(new_password)