Python Module-3 QB Solution (21EC643)
Python Module-3 QB Solution (21EC643)
Module -3
Question Bank Solution
1. Explain the finding patterns of text without regular expressions with example.
Soln: Finding Phone Number from a Given Text without using regular expression> instead
using if else statements
Example:
To find phone numbers with the format (XXX-XXX-XXXX) within a given text.
The phone number format consists of 12 characters: the first 3 are digits, followed by a hyphen,
then 3 digits, another hyphen, and finally 4 digits.
Program:
def PhoneNumber(text):
if len(text) != 12:
return False
for i in range(0, 3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4, 7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8, 12):
if not text[i].isdecimal():
return False
return True.
#function call
text= “415-555-4242 ”
Z=PhoneNumber(text)
Output : True
text= “Dhoni-123 ”
Z=PhoneNumber(text)
Output : False
Explanation
1. Function Definition: PhoneNumber
2. Check if the string length is 12 characters. If not, return False.
3. Verify the first 3 characters are digits. If not, return False.
4. Check if the 4th character is a hyphen ('-'). If not, return False.
5. Verify characters 5 to 7 are digits. If not, return False.
6. Check if the 8th character is a hyphen ('-'). If not, return False.
for i in range(len(message)):
chunk = message[i :i+12]
if PhoneNumber(chunk):
print(f 'Phone number found: {chunk}’)
With example, explain the following Pattern Matching with Regular Expressions.
Soln:
Accessing Groups:
o After performing a search with search() method, use group() method to retrieve
specific sub groups:
▪ mo.group(0)( (equivalent to mo.group()) retrieves the entire matched text.
▪ mo.group(1) retrieves the matched first group.
▪ mo.group(2) retrieves matched second group.
Program:
import re
#print result
print(f"Area code : {area_code}\n Main number : {main_number} \nEntire
number : {entire_no}")
o The | character is used to match one of many objects in the text or string.
o It is kind of OR operation.
o Syntax: r 'Ramesh | Mahesh'
o If both 'Ramesh' and 'Mahesh' are in the string, the first occurrence is matched.
import re
Str1='Ramesh Mahesh Suresh Kalmesh'
Str2='Mahesh Ramesh Suresh Kalmesh'
pattern = r'Ramesh|Mahesh'
comp= re.compile(pattern)
mo1=comp.search(Str1)
mo1_g= mo1.group() if mo1 else “No match”
print(mo1_g) # Output: Ramesh
mo2=comp.search(Str2)
mo2_g= mo2.group() if mo2 else “No match”
print(mo2_g) # Output: Mahesh
Pattern= r'Bat(man|mobile|copter|bat)'.
• This way, we can specify the common prefix 'Bat' only once.
Program:
import re
# Retrieve the full matched text and the part inside parentheses
mo1_g = mo1.group() # Full match
mo1_g2 = mo1.group(3) # Part inside parentheses
import re
mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo2 else “no match”
print(mo2_g) # Output: Batwoman
Mo3 = comp.search(Str3)
Mo3_g = mo3.group() if mo3 else “no match”
print(mo3_g) # Output: None
except :
print(“No match”)
Example -2
import re
try:
#for string 1
# Use the search method to search for the pattern
mo1 = compl.search(String1)
mo1_g=mo1.group() if mo1 else “no match”
print(mo1_g)
mo2 = compl.search(String2)
mo2_g=mo2.group()
print(mo2_g)
except:
print(“None”)
try:
# Use the search method to search for the pattern
mo1 = comp.search(Str1)
mo1_g = mo1.group() if mo1 else “no match”
mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo2 else “no match”
print(mo2_g) # Output: Batwoman
mo3 = comp.search(Str3)
mo3_g = mo3.group() if mo3 else “no match”
print(mo3_g) # Output: Batwowowowoman
except:
print(“No match”)
import re
mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo1 else ‘No match’
print(mo2_g) # Output: Batwoman
mo3 = comp.search(Str3)
mo3_g = mo3.group() if mo1 else ‘No match’
print(mo3_g) # Output: Batwowowowoman
import re
Str1='Hello world!'
Str2='He said hello.'
Pattern =r'^Hello'
comp= re.compile(pattern)
mo2=comp.search(Str2)
mo2_g= comp. group() if mo2 else ‘No match’
print(mo2_g) Output: ‘None’
import re
Str1='Hello world!'
Str2='He said Hello'
Pattern =r 'Hello$'
comp= re.compile(pattern)
mo1=comp.search(Str1)
mo1_g= mo1. group() if mo1 else ‘None’
print(mo1_g) Output: Hello
mo2=comp.search(Str2)
mo2_g= mo2. group() if mo2 else ‘None’
print(mo2_g) Output: ‘None’
import re
Str1='1234567890'
Str2='12345xyz67890'
Pattern = r'^\d+$'
comp= re.compile(pattern)
mo1=comp.search(Str1)
mo1_g= mo1. group() if mo1 else ‘No match’
print(mo1_g) Output: 1234567890
mo2=comp.search(Str2)
mo2_g= mo2. group() if mo1 else ‘No match’
print(mo2_g) Output: No match
▪ These symbols are essential for defining precise patterns in text matching using regular
expressions.
• The dot character (.) can be used to match any single character in a string, such as a letter,
number, or symbol.
• Pattern a.b would match aab, a9b, a-b, or any three-character string where the first character
is 'a', the second is any character, and the third is 'b'.
import re
Example:
import re
# Define pattern t
pattern = r'First Name: (.*) Last Name: (.*)'
Write a Python program that uses regular expressions to find all instances of a number followed by
a word in a given string. Your program should output these matches. Use the following string for
testing:
Str = '12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6
geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge'
import re
In this example, the regular expression \d+\s+\w+ matches text with one or more numeric digits
(\d+), followed by a whitespace character (\s), followed by one or more word characters (\w+). The
findall() method returns all matching objects of string.
Write a Python program that uses re.VERBOSE to find phone numbers in a given text. The
program should recognize various phone number formats, including those with area codes in
parentheses, different separators, and optional extensions. Use the following text for testing:
▪ Regular expressions can get complicated when dealing with complex text patterns.
▪ To make them more readable, we can use "verbose mode" with re.VERBOSE, which allows,
whitespace and comments inside the regex string.
Program:
import re
# String or Text
text = 'Call me at (123) 456-7890 or 123-456-7890 ext. 1234.'
# Define the regex pattern with comments and spread over multiple lines
pattern = r'''(
(\d{3} | \(\d{3}\))? # area code
(\s |- |\.)? # separator
\d{3} # first 3 digits
(\s |- |\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)'''
# Print matches
for match in matches:
print(match)
For the given a string, text = 'India won the twenty twenty world cup 2024'.
Write a Python program to perform the following tasks using regular expressions (regex):
i. Create and compile a regex pattern to find all vowels (both lowercase and uppercase) in the
given string.
ii. Create and compile a regex pattern to find all alphanumeric characters (both letters and
numbers) in the given string
import re
# Define pattern
Pattern1 = r'[aeiouAEIOU]'
Pattern2 = r'[a-zA-Z0-9]'
#for pattern1
Mo1 = Comp1.findall(text)
print(Mo1) # Output: ['I', 'i', 'a', 'o', 'e', 'e', 'e', 'o', 'u']
#for pattern2
Mo2 = Comp2.findall(text)
print(Mo2)
# Output: 'I’n’d’I’a’ w’o’n’ t’h’e’ t’w’e’n’t’y’ t’w’e’n’t’y’ w’o’r’l’d’ c’u’p'
Program
import re
Program:
import re
import re
# text or string
text = 'Agent Virat gave the secret documents to Agent Dhoni.'
comp = re.compile(pattern)
print(result)
# Output: 'CENSORED gave the secret documents to CENSORED.'
Program:
import re
# String or Text
text = 'Call me at (123) 456-7890 or 123-456-7890 ext. 1234.'
# Define the regex pattern with comments and spread over multiple lines
pattern = r'''(
(\d{3} | \(\d{3}\))? # area code
(\s |- |\.)? # separator
\d{3} # first 3 digits
(\s |- |\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)'''
# Print matches
for match in matches:
print(match)
1. Combine Flags: Use the bitwise OR operator (|) to combine re.IGNORECASE, re.DOTALL, and
re.VERBOSE.
2. Compile with Combined Flags: Pass the combined flags as the second argument to
re.compile().
Program
import re
# Text to search
text = "Hi\nHI\nHi"
# Print matches
print(matches) # Output: ['Hi', 'HI', 'Hi']
Explain search() and findall() method in regex. Also write the difference between search()
function and findall() function in regex
• The search() method scans the entire string and looks for matching object. It returns only
first matching object.
• It returns a match object (mo) if it finds match, otherwise it returns None.
• We have to use the group() method to get the matched object
import re
findall() Method:
• The findall() method scans through the entire string and finds all occurrences of the
pattern.
• It returns a list of all matched substrings.
import re
File Handling
Explain the concept of file path. Also discuss absolute path and relative path.
File Path Definition:
• A file path is the specific location of a file or folder within a file system.
• It shows where a file is located starting from the root directory of the operating system.
os.path.join()
It is used to construct a path by joining one or more path components.
Example:
os.path.join('usr', 'bin', 'spam')
Output (windows): usr\bin\spam
Output (linux): usr/bin/spam
Useful for constructing file paths programmatically.
os.path.basename()
• It extracts the last component of a path, which typically represents the filename.
• Example: os.path.basename('C:\\Windows\\System32\\calc.exe')
• Output: calc.exe
Explain the concept of file handling. Also explain reading and writing process with suitable
example
File handling in Python refers to operations performed on files, such as reading or writing to them.
File operation takes place in the following order:
o Open a file
o Read or Write (Perform operation)
o Close the file
Arguments:
filename: Name of the file to be opened. Pathname of the file is optional if file is in
current working directory.
f1: when we open file using open() function, python returns the file object. This file object
is stored in the variable f1, this is also called as handle. A file object represents a file in our
computer; it is simply another type of value in Python, much like the lists or dictionaries
This file object can then be used to perform operations (read/write) on the file.
mode: We have to specify what is purpose of opening a file (for reading or writing etc).
Reading Files
• Consider the text file with name “myfile” written in notepad and stored in current directory.
Reading file using read() function.
read ()
This function is used to read the entire content of the file (if the argument is not mentioned)
read(n):
• It reads a specified number of characters (or bytes) from the file.
• For example, f.read(5) will read the first 5 characters from the file.
• If you do not specify a number inside the parentheses, read() will read the entire file.
Output:
• Here in this case, it reads the entire file at once and stores in the variable “d1”. So, this
method is suitable when the file size is very less.
Output:
Hello python \n',
Welcome python\n',
Hello India \n',
How are you \n',
Writing Files
• we can use write() method to write data into a file.
• write() can be used in 2 modes: mode “w” or mode “a”.
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
• If the file does not exist, then a new file (with the given name) will be created.
Example 1: To append
This method will append text to the end of the existing file. No overwriting in case of append
Open the file "myfile.txt" and append content to the file:
f = open("myfile.txt", "a")
f.write("Hello Virat \n")
f.write(“ Hello Dhoni”)
f.close()
Example 2: To write
This method will over-write on the all-ready existing data
#Open the file "myfile.txt" and overwrite the content:
f = open(" myfile.txt", "w")
f.write("Hello Pandya")
f.close()
#open and read the file after the writing :
f = open("myfile.txt", "r")
print(f.read())
Develop a Python program to read and print the contents of a text file
# Program to read and print the contents of a text file named myfile.txt
Explain reading and saving python program variables using shelve module with suitable Python
program
• We can save variables of Python programs in binary shelf files using the shelve module.
• Binary shelf files are used to store data in a binary format.
• Example: After running a program and configuring some settings, we can save these
settings into a binary shelf file. Later, when we want to run the program again, we can load
the settings from the file instead of configuring them again.
• The shelve module allows us to implement Save and Open features in our programs.
To retrieve data
# Open the same shelf file
shelf_file = shelve.open('mydata')
To convert to list
• Just like dictionaries, shelf values have keys() and values().
• This shelve method will return list-like values but not the true lists
• To get true lists, pass the returned values to the list() function.
shelf_file = shelve.open('mydata')
# Get keys and values from the shelf file, and convert them to lists
keys_list = list(shelf_file.keys())
values_list = list(shelf_file.values())
print(f"Keys: {keys_list}")
print(f"Values: {values_list}")
• The pprint.pformat() function from the pprint module is used to convert complex data
structures into a formatted string, which is easy to read.
import pprint
Develop a Python program to find the total size of all the files in the given directory.
To find the total size of all the files in the directory,
In this case, use os.path.getsize() and os.listdir() together