Python - Week 7 - Strings, Regular Expressions, Assignment Operator, None


Lets explore Python constructs related to strings.

Here is the first problem:

A website requires the users to input email address and password to register.
Write a program to check the validity of password input by users.
Following are the criteria for checking the password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
3. At least 1 letter between [A-Z]
4. At least 1 character from [$#@]
5. Minimum length of transaction password: 6
6. Maximum length of transaction password: 12
Your program should accept a sequence of comma separated passwords and will check them according to the above criteria.
Passwords that match the criteria are to be printed, each separated by a comma.
Example
If the following passwords are given as input to the program:
ABd1234@1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
ABd1234@1


Notes on the implementation:

  1. None of the string methods accepts patterns—for pattern-based text processing, you must use the Python re standard library module.
  2. We use re module to parse the password as a regular expression.
  3. re searches for different combinations which meet the criteria like a-z, A-Z, presence of $, #, @. But we can have more complicated expressions if required.
  4. None, which is always considered to be false is the only value of a special data type in Python and typically serves as an empty placeholder (much like a NULL pointer in C).
  5. None is also the default return value of functions that don’t exit by running into a return statement with a result value. This is what we observe in re search calls.
  6. We pass the passwords as a tuple. Tuples are ordered collections of arbitrary objects. Like lists, tuples are best thought of as object reference arrays

As a follow up to above problem:


Assuming that we have email addresses in the "username@companyname.com" format, write program to print the user name and domain name of a given email address.

Example:
If the following email address is given as input to the program:

john@google.com

Then, the output of the program should be:

Name : john
Domain : companyname


This program uses some more Python ways of doing things:

  1. split function in Lines 13 and 17, splits the string  into a list based on an optional separator.
  2. In Line 13, we see one more Python construct. Python assigns items in the sequence on the right to variables in the sequence on the left by position, from left to right

Comments

Popular posts from this blog

Python - Week 8 - Dictionaries, Tuples, Sorting and Filtering

Python - Week 3 - Lists, Tuples, Slicing, Math Functions

Python - Week 5 - Comprehensions