You are on page 1of 7

1) Write a Python program to search for patterns in the string below and print matched if the pattern is found

and
notmatched when not found (print the pattern and matched or unmatched in the output.) :
string = 'The quick brown fox jumps over the lazy dog.'
patterns = [ 'fox', 'dog', 'horse' ]
2) Write a Python program to search for patterns in the string below and print the location of the pattern in the original string
(print the pattern and location in the output.) :
string = 'The quick brown fox jumps over the lazy dog.'
pattern1 = 'fox'
pattern2 = 'dog'
3) Use regular expression to change the input string to the output forms given below:
Inputtext: The quick brown fox jumps over the lazy dog.
Output1: The quick brown wolf jumps over the lazy dog.
Output2: THE quick brown FOX jumps over the lazy DOG.
4) Write a Python function that matches a string that has an 'a' followed by anything, ending in 'b'. Test this on the following
strings:
string1 = "aabbbbd"
string2 = "aabAbbbc"
string3 = "accddbbjjjb"
The output should clearly tell match or nomatch for each string.
5) Write a Python program to find all words starting with 'a' or 'e' in a given string.
string = "Astounding aardvarks, arguably an ancient animal, always ate apples and acorns and artichokes all around
Athens, amazing. Eating enough early eases elimination."
Use Regular expressions to solve the following

6) Write a Python program to replace maximum 2 occurrences of space, comma, or dot with a colon.
string = "text = 'Python Exercises, PHP exercises, C++ exercises’
 
7) Write a Python program to find all five characters long word in a string.
string = 'The quick brown fox jumps over the lazy dog.'
 
8) Write a Python program to find all words which are at least 4 characters long in a string.
string = 'The quick brown fox jumps over the lazy dog.'
 
9) Write a Python program to find all adverbs and their positions in a given sentence. Can you do the same for nouns?
string = "Clearly, he has no excuse for such behavior."
 
10) Write a Python program to split a string at uppercase letters.
string = "PythonTutorialAndExercises”
11) Write a Python program to find all words starting with 'a' or 'e' in a given string.
string = "Amazing ancient animals eat apples in Egypt. ”
12) Write a Python program to find all adverbs and their positions in a given sentence. Can you do the same for nouns?
string = "Clearly, he has no excuse for such behavior."
13) Split the following input string into equal groups. Use modulo operator to decide the number of groups. The input string is "BEST OF LUCK". Hint:
Beware of the spaces in a string.
14 Write a program to Check whether the characters in string s2 and s3 are present in s1 and the output should give the information of presence
either by printing “yes or no” or by writing “1 or 0”. s1 = "IAMHERE", s2 = "IAM", s3 = "NOT"
15) Create a file input.txt with the following contents. Write a program to read the file and find the length of each line after stripping the end-line
character and write it into a new file in a tab delimited format.
DO NOT CHANGE THE CONTENTS. BEWARE OF EXTRA LINES WHEN CREATING THE FILE, YOUR PROGRAM WILL GIVE ERROR IF YOU HAVE EMPTY
LINES IN THE FILE UOU CREATED
The input.txt file should have the following lines:
Mercury
Venus
Mars
Jupiter
Earth
16) Write a program to print the items in the input_list in a reverse order and write it out to a file.
Input_list = ['hi', 'you', 'guys', 'are', 'doing', 'great’]
17) Write a program to print the only the numbers and not characters from the string below
Input_string = "Hello 12345 World”
18) Convert the items in the input_list to lower case using list comprehension.
Input_list = [“AB”,”RA”,”CA”,”DA”,”BR”,”A”]
3) Loop through the items in the fruits list
Complete these loops fruits = ["apple", "banana", "cherry"]
___x__ fruits__ :
print(x)
1) Print i as long as i is less than 6
i=1 4) Loop through the items in the fruits list
___i < 6 __
fruits = ["apple", "banana", "cherry"]
print(i) for x in fruits:
i += 1 if x == "banana":
___________
2) Stop the loop if i is 3 print(x)

i=1 (5) Use an appropriate function to loop through a


while i < 6: code set 6 times.
if i == 3: for x in ______:
_____ print(x)
i += 1
• Dictionaries are objects like lists. They also have a set of methods like lists. Please use
the dictionary below to practice the dictionary methods.
scientists = {'Newton' : 1642, 'Darwin' : 1908, 'Turing' : 1912}
1) Print the keys, values and items of this dictionary
2) Add items to this dictionary. For example, add ‘country’ : “England” into the
dictionary scientists.
3) Update the dictionary scientists with the dictionary temp where,
temp = {'Curie' : 1687, 'Hopper' : 1906, 'Franklin' : 1920}
4) Use for loop to read the dictionaries temp and scientists and print the keys if the
first 2 digits of the values match.

Write a function or method using if statement to check whether given key exists in the
following dictionary.
dict = {'a': 100, 'b':200, 'c':300}
• Check if the keys a, c and w exists in this dictionary.
Write a script to read a file line by line and store them in a list
Create the following file with filename1: test.txt
python
Bioinformatics
Computational Biology
Genetics
Immunology
Every_thing_in_Biology
(a) Store these lines and print it as a list using a python script
Create the following file with filename2: Assignment_input.txt
python
Bioinformatics
Computational Biology
Every_thing_in_Biology

(b) Read the contents of the files: test.txt and Assignment_input.txt


write only the lines common to both the files
Create the following file with filename1: test.txt
python
Bioinformatics
Computational Biology
Genetics
Immunology
Every_thing_in_Biology
Create the following file with filename2: Assignment_input.txt
Programming_language
Field_in_Biology1
Field_in_Biology2
Field_in_Biology3
Field_in_Biology4
Every_thing_in_Biology
Read the contents of the files: test.txt and Assignment_input.txt
write lines from both the files such that Lines of file1 and Lines of file2 are delimited by \t
Eg: python Programming_language
Bioinformatics Field_in_Biology1

You might also like