
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check whether given password meets criteria or not in Python
Suppose we have a string s, representing a password, we have to check the password criteria. There are few rules, that we have to follow −
- Password length will be At least 8 characters and at most 20 characters long.
- Password contains at least one digit
- Password contains at least one lowercase character and one uppercase character
- Password contains at least one special character like !"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~
- Password does not contain any other character like tabs or new lines.
So, if the input is like "@bCd12#4", then the output will be True.
To solve this, we will follow these steps −
- a:= 0, b:= 0, c:= 0, d:= 0
- if size of password < 8 or size of password > 20, then
- return False
- for each character i in password, do
- if i is uppercase letter, then
- a := a + 1
- otherwise when i is lowercase letter, then
- b := b + 1
- otherwise when i in these set of special characters '"!"#^modAND\'() *+,- ./:;<=>?@[\]XOR_`{OR}~"', then
- c := c + 1
- otherwise when i is a digit, then
- d := d + 1
- if i is uppercase letter, then
- if a>=1 and b>=1 and c>=1 and d>=1 and a+b+c+d is same as size of the password , then
- return True
- otherwise,
- return False
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, password): a=0 b=0 c=0 d=0 if len(password)<8 or len(password)>20: return False for i in password: if i.isupper(): a+=1 elif i.islower(): b+=1 elif i in '"!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~"': c+=1 elif i.isdigit(): d+=1 if a>=1 and b>=1 and c>=1 and d>=1 and a+b+c+d==len(password): return True else: return False s = "@bCd12#4" ob = Solution() print(ob.solve(s))
Input
"@bCd12#4"
Output
True
- Related Articles
- C++ Program to check whether given password is strong or not
- Program to check whether given graph is bipartite or not in Python
- Python program to check whether a given string is Heterogram or not
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether given tree is symmetric tree or not in Python
- Program to check whether given list is in valid state or not in Python
- C++ program to check whether given string is bad or not
- Program to check whether given words are maintaining given pattern or not in C++
- Program to check whether parentheses are balanced or not in Python
- Java program to check whether a given string is Heterogram or not
- C# program to check whether a given string is Heterogram or not
- Swift Program to check whether a given string is Heterogram or not
- Program to check whether elements frequencies are even or not in Python
- Program to check whether two sentences are similar or not in Python

Advertisements