Assignment 5 : Exception Handling and Regular Expression
Lab Assignments
SET A
1. Write a Python program to demonstrate the zero division error and overflow error.
x=int(input("enter first value:"))
y=int(input("enter second value:"))
try:
result=x/y
except ZeroDivisionError:
print("Division by Zero")
else:
print("Result is:",result)
finally:
print("Execute finally clause")
2. Write a Python program to find sequences of lowercase letters joined with a underscore
import re
def text_match(text):
patterns = '^[a-z]+_[a-z]+$'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')
print(text_match("aab_cbbbc"))
print(text_match("aab_Abbbc"))
3. Write a python program to Check if String Contain Only Defined Characters using Regex
import re
def match(str,pattern):
if re.search(pattern,str):
print("valid string")
else:
print("invalid string")
pattern = re.compile('[A-Z]+$')
match('ABCDE',pattern)
match('12ABCda',pattern)
match('12345',pattern)
SET B
1. Write a Python program to match a string that contains only upper and lowercase letters,
numbers, and underscores.
import re
def text_match(text):
patterns = '^[a-zA-Z0-9_]*$'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')
print(text_match("The quick brown fox jumps over the lazy dog."))
print(text_match("Python_Exercises_1"))
2. Write a python Program to Remove duplicate words from Sentence
def unique_list(text_str):
l = text_str.split()
temp = []
for x in l:
if x not in temp:
temp.append(x)
return ' '.join(temp)
text_str = "Python Exercises Practice Solution Exercises"
print("Original String:")
print(text_str)
print("\nAfter removing duplicate words from the said string:")
print(unique_list(text_str))
3. Write a python to Remove all characters except letters and numbers
import re
ip_str = input("Enter the string: ")
# Using re.sub
op_result = re.sub('[\W_]+', '', ip_str)
print ("Output string is:", op_result)
OR:-
import re
ip_str = input("Enter the string: ")
# Using re.sub
op_result = re.sub('[\W_]+', '', ip_str)
print ("Output string is:", op_result)
PROGRAMS FOR PRACTICE:
1. Write a python program to Count Uppercase, Lowercase, special character and numeric values using Regex
2. Write a python program to find the most occurring number in a string using Regex
3. Write a python Regex to extract maximum numeric value from a string
4. Write a python program to put spaces between words starting with capital letters using Regex
5. Write a python to Check whether a string starts and ends with the same character or not
6. Write a python regex to find sequences of one upper case letter followed by lower case letters
7. Write a python Regex program to accept string ending with alphanumeric character
8. Write a python Regex program to accept string starting with vowel
9. Write a python Program to check if a string starts with a substring using regex
10. Write a python Program to Check if an URL is valid or not using Regular Expression
11. Write a python Program to Parsing and Processing URL using Python – Regex
12. Write a python Program to validate an IP address using ReGex else:
print "Nothing found!!"
When the above code is executed, it produces following result −
searchObj.group() : Cats are smarter than dogs
searchObj.group(1) : Cats
searchObj.group(2) : smarter
13. Write a python Program to Check if email address valid or not
14. Write a python program to find files having a particular extension using RegEx
15. Write a python program to extract IP address from file
16. Write a python program to check the validity of a Password
Students can practice Common Examples of Exception as:
1. Division by Zero
2. Accessing a file which does not exist.
3. Addition of two incompatible types
4. Trying to access a nonexistent index of a sequence
5. Removing the table from the disconnected database server.
6. ATM withdrawal of more than the available amount
No comments:
Post a Comment