Validation. What does this word mean? How important validation is?
To begin with, one literal meaning of Validation is, “the action of checking or proving the validity or accuracy of something.“
Now the definition itself explains the significance of this word. To check or prove the accuracy or validity or authenticity of something. Example, say, a password or User-ID or may be email-ID. In simple words we can say that validation is to check whether the string input is in correct (prescribed) form or not. One thing to understand here is that string validation will only check that that string is in correct format. It will not check whether the string itself is correct or not.
Say, you are asked to make a password that is a combination of 4 words and 2 digit only. Let the password be: mark40. Using string validation you can check this. If you make a password, say, mark411, it’s violating the validation rule. An error message will prompt. But string validation will not tell anything whether mark40 is your password. That’s the Database Management part. That’s why i have underlined the word make. After making it is the duty of respective database to check whether given password is yours or not.
So, on this big day (i mean Day 3, no National Holiday), we are going to cover some very useful string validation operations using RegEx. Our final Task of Day 3 will be the following task:
# Everybody has once (or many a times infact), got chance to visit a site and make a password which prompts a message like this:
- Your password must be at least 8 characters long.
- Your password must contain at least 1 number and 1 special character.
- Password must have 1st character in uppercase.
Write a program to code this out.
To do the above task, first we must do some little warm up exercises.
#Task 1. Write a program to simply generate a password which consists of numbers and characters?
What should be our approach?
- Enter a string
- The string should contain digits. \d will help.
- The string should contain non-digits. \D will help.
- Step 2 and 3 can be collectively said to be alpha-numeric. This we have not studied. Our Table 1 might help.
- Finally, print the result with some message.
The code goes like this:

1st two lines of code are mandatory.
3rd line of code will ask the user to enter the input string which is the password.
4th line of code: Referring Table 1 we have another special sequence \w which is used for alphanumeric characters. So here we will use \w.
5th line of code will simply match the pattern generated in 4th line with the input string and save the result in variable called result. If match is correct if loop will execute or else loop will execute.
Let’s run the code:

I gave the input string stark. It’s a match. Let’s analyze \w more. \w is equivalent to [a-zA-Z0-9]. What does this even mean? What if i write my password in all uppercase?

It’s working. What if i give password containing only digits?

Again it worked. What if i write a password containing characters in upper and lower case and digits also?

This is also working cool. We have not played with the code at all. We have only changed the password. And every time it’s working correctly. So, what is the power of \w? \w is a combination of lowercase characters [a-z], uppercase characters [A-Z] and numeric values [0-9]. And, collectively it is equivalent to [a-zA-Z0-9]. That’s a powerful special sequence to use to. What about any special character?

Oh!! Thank GOD!! Finally, the else loop got executed. I was wondering that else loop is only consuming lines here.
It also means that \w do not support special characters. But none the less, it’s the most powerful special sequence we have used till now. \w can not only substitute the independent need of \d and \D but also gives a scope of using \d and \D collectively. What about special characters now? If \w checks for everything which is alphanumeric, then, \W should check for everything which is not alphanumeric. Running the above code by replacing \w with \W, will give,

That’s cool. Definitely, \w will play a role in our final task. \w is equivalent to [a-zA-Z0-9]. Can we replace \w with [a-zA-Z0-9] ?

Yup, we can replace. Now, how to write special characters along with this?

So, simply writing some special characters (or all, depending) along with [a-zA-Z0-9] will get the job done. Now this is an important feature because you might have seen certain login pages which gives the message, “. or _ not allowed”. If we write [!@#$%^&*,/\?<>~] it will not allow . or _ in your password. This is the back end part of how it is done.
Moving on, let’s add one more parameter in our Task 1. Our password must be of certain characters long.
# Task 2.Write a program to simply generate a password which consists of numbers and characters and it should be at least 6 characters long ?
Approach should be same as of Task 1. Fortunately, we have studied \w by now. What about length issue? What i have to do is to check the length of my input string. It must not go beyond 6 characters. Python provides a function called len (). Nothing better than to code and see.

Here, i have given a condition in if loop that if length of my input string is less than 6 then a message will be generated that Password must be atleast 6 characters long. In 7th line of code i have used {6, }. This will check for the length of password which starts from 6 to any range. If loop will check that password must be atleast 6 characters long. But how long,{6, } will check.
Can I put a limit to the upper size of this length? Yes. Your password can be of specific length as well. Say, only characters long. Simply write {1, 6}.

One caution!! When we use {1,6}, any character written after 6th position will be excluded.

Finally, after these tremendous warm-up exercises, we are ready to proceed with our final task of Day 3. Recalling it, the Task was:
- Your password must be at least 8 characters long
- Your password must contain at least 1 number and 1 special character.
- Password must have 1st character in uppercase

It’s combination of all the above exercises which we just did. The new part is 4th If condition. It says that if the 1st character of input string (written as [0]th position) is not in uppercase then prompt a message. Why this line is necessary? This is because, we are using [a-zA-Z0-9]. It consists of uppercase character as well. If accidentally someone writes the 1st character in uppercase then its OK OK. But if not, then this loop will help.

Now I have a strong password with a capital letter, small letter, numbers and special characters. This is how password validation is done in login pages.
One final shot. I made a strong password. But I got an intuition that while I was making my password, someone was peeping into my computer. What if when i type everything appear in form of dots, so that only i know it.
For this, Python provides a module known as getpass (). getpass() prompts the user for a password without echoing. Only additional two lines are required.
1st line is to import getpass
2nd line is to write getpass.getpass(“Enter the password: “), instead of, input (“Enter the password: “)

getpass () is a built in method to improve the security while typing itself. See the 2nd line and 5th line of code.
This brings to the closure of our Day 3 learning session of RegEx. Have a nice practice time ahead.