Practical 2
NAME- SAJID AHMED Roll no-001 DATE-4/2/2023
Aim: Programs based on regular expressions
1. Using regular expression write the Python code to accept a string from
user and search whether it ends with “tion”.
CODE:
import re as r
s=input("Enter a String: ")
a=r.findall("tion$",s)
if a:
print("Found")
else:
pr8int("Not found")
OUTPUT:
2. Given a string “abbreviation” Using regular expression write the Python
code to accept alphabets from user and find whether the alphabets are part
of the string.
CODE:
import re as r
t="Abbreviation"
b=input("Enter a Alphabet: ")
a=r.findall(b,t)
if a:
print("Yes Alphabet found")
else:
print("Alphabet not found in string")
OUTPUT:
1
3. Using regular expression write the Python code to accept a string from
user and find whether it starts with ‘mo’ and ends with ‘le’ and any 2
characters in between them.
CODE:
import re as r
t=input("Enter a string: ")
a=r.findall("mo..le",t)
if a :9
print("Found")
else:
print("Not found")
OUTPUT:
4. Using regular expression accept a string from user and display the
number of times alphabet ‘a’ is present in it.
CODE:
import re as r
c=input("Enter a string: ")
a=r.findall("a+",c)
print(len(a))
OUTPUT:
2
5. Using regular expression write the Python code to accept a string from
user and check whether it has any vowels or number 6,7,8,9.
CODE:
import re as r
c=input("Enter a string: ")
a=r.findall("[a,e,i,o,u,6,7,8,9]",c)
if a:
print("Found")
else:
print("Not Found")
OUTPUT:
6. Using regular expression write the Python code to accept a string from
user and check whether it ends with a number.
CODE:
import re as r
c=input("Enter a string: ")
a=r.findall("[1,2,3,4,5,6,7,8,9,0]$",c)
if a:
print("Found")
else:
print("Not Found")
OUTPUT: