Get startedGet started for free

Building a password validator

Your team is developing an authentication system for a website. To ensure user accounts are secure, you need to create a validate_password() function that checks if passwords meet minimum security requirements. The function should verify that a password is at least eight characters long and contains at least one special character from the string module's punctuation collection.

The string module and a test password variable user_password are already imported for you.

This exercise is part of the course

Intermediate Python for Developers

View Course

Exercise instructions

  • Complete the validate_password() function to check if the password is at least 8 characters long.
  • Add a loop to check if any character in the password exists in string.punctuation.
  • Call the function with user_password, and store the result in is_valid.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def validate_password(password):
    # Check if password is at least 8 characters long
    if ____(____) >= 8:
        # Check if password contains a special character
        for char in password:
            if char in string.____:
                return True
    return False

# Call the function and store the result
is_valid = ____(____)
print("Is the password valid? ", is_valid)
Edit and Run Code