Day 1 - Python

Let's start the first program with the most common Password Checker!

Create a program that prompts the user to input a password and checks if it meets the criteria: at least 8 characters long, containing at least one uppercase letter, one lowercase letter, and one symbol from {!@#$%^&*()-+}. Then, print "Valid password" if it meets the criteria, otherwise print "Invalid password".


Problem Statement: Password Validation

Design a program that verifies whether a given string password meets enhanced criteria. The enhanced criteria for a valid password are as follows:

  • The password must be at least 8 characters long.
  • The password must contain at least one uppercase letter.
  • The password must contain at least one lowercase letter.
  • The password must contain at least one symbol from the set {!@#$%^&*()-+}.

Your task is to implement a program that takes a string password as input and checks if it meets the specified enhanced criteria. If the password meets the criteria, the program should print "Valid password"; otherwise, it should print "Invalid password".


Input:

A string representing the password.

Output:

"Valid password" if the password meets the enhanced criteria.

"Invalid password" if the password does not meet the enhanced criteria.

Note:

Assume that the password contains only printable ASCII characters.

Example:

Input: "Passw0rd!"

Output: "Valid password"

Input: "abc123"

Output: "Invalid password"


SOLUTION

import re

pwd = input("Enter your password: ")


if len(pwd) >= 8 and re.search("[a-z]", pwd) and re.search("[A-Z]", pwd) and re.search("[!@#$%^&*()-+]", pwd):

    print("Valid password")

else:

    print("Invalid password")


Insights

  • This coding used the Regular Expression Module.
  • Revise the concepts of logical operators. Understand the difference between 'and' and 'or'
  • Read the documentation of the Regular Expression https://docs.python.org/3/howto/regex.html. Explore more functions!
  • Try exploring to check for spaces.
  • Explore with this piece of code, without re module, as one single problem can be solved in n number of ways! Try exploring various methods!
if len(pwd) >= 8 and any(char.isdigit() for char in pwd) and any(char.isalpha() for char in pwd)

Explore and Enjoy! :)


Comments

  1. Amazing tutorial. It was very helpful to me because I started python only a month ago!

    ReplyDelete
  2. Simply the best! This is actually an easier explanation compared to other sites. They give you the history of Python, and then they teach you stuff. This is amazing. Looking forward to seeing a course on HTML.

    ReplyDelete
    Replies
    1. Thank you so much Rohan! Definitely let's explore more in frontend also! :)

      Delete

Post a Comment