👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Basic conditions for password verification:
- At least 8 characters.
- Letters must be between [az]
- At least one alphabet must be uppercase [AZ]
- At least 1 digit or digit between [0-9].
- At least 1 character from [_ or @ or $].
Examples:
Input: R @ m @ _f0rtu9e $ Output: Valid Password Input: Rama_fortune $ Output: Invalid Password Explanation: Number is missing Input: Rama # fortu9e Output: Invalid Password Explanation: Must consist from _ or @ or $
Here we have used the re module which provides support for regular expressions in Python . Along with this the re.search () method returns False (if the first parameter is not found in the second parameter). This method is best for validating a regular expression rather than extracting data. We used re.search () to check if alphabets, numbers or special characters are correct. To check for spaces, we use "/ s" which is included in the regular expression module.
# Python program to check if the password is correct # Regular expression module used with search () import re password = "R @ m @ _f0rtu9e $ " flag = 0 while True : if ( len (password) & lt; 8 ): flag = - 1 break elif not re.search ( "[az]" , password): flag = - 1 break elif not re.search ( "[AZ]" , password): flag = - 1 break elif not re.search ( "[0-9]" , password): flag = - 1 break elif not re.search ( "[_ @ $]" , password): flag = - 1 break elif re.search ( "s" , password): flag = - 1 break else : flag = 0 print ( "Valid Password" ) break if flag = = - 1 : print ( "Not a Valid Password" ) |
Output:
Valid Password
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Python program to validate password, check other ast Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
Boris Galleotti
Boston | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - Python program to validate password in Python is not the simplest one. I am just not quite sure it is the best method
Dmitry Krasiko
Warsaw | 2023-03-21
Simply put and clear. Thank you for sharing. Python program to validate password and other issues with io Python module was always my weak point 😁. I am just not quite sure it is the best method
Boris Krasiko
Boston | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - Python program to validate password in Python is not the simplest one. I am just not quite sure it is the best method
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically