Get startedGet started for free

Invalid password

The second part of the website project is to write a script that validates the password entered by the user. The company also puts some rules in order to verify valid passwords:

  • It can contain lowercase a-z and uppercase letters A-Z
  • It can contain numbers
  • It can contain the symbols: *, #, $, %, !, &, .
  • It must be at least 8 characters long but not more than 20

Your colleague also gave you a list of passwords as examples to test.

The list passwords and the module re are loaded in your session. You can use print(passwords) to view them in the IPython Shell.

This exercise is part of the course

Regular Expressions in Python

View Course

Exercise instructions

  • Write a regular expression to check if the passwords are valid according to the description.
  • Search the elements in the passwords list to find out if they are valid passwords.
  • To print out the message indicating if it is a valid password or not, complete .format() statement.

Hands-on interactive exercise

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

# Write a regex to check if the password is valid
regex = ____"[____]____"

for example in passwords:
  	# Scan the strings to find a match
    if re.____(____, ____):
        # Complete the format method to print out the result
      	print("The password {____} is a valid password".format(pass_example=____))
    else:
      	print("The password {____} is invalid".format(pass_example=____))   
Edit and Run Code