DATE:01-07-2025
PRACTICAL OBJECTIVE & SOLUTIONS
NO.
Write a program in python to create a text file “sentence.txt” and functions
as given below:
7 moveCharacters( ) -to create three files “lower.txt” which contains
all the lowercase letters and “upper.txt” which contains all the
uppercase letters and “digit.txt” which contains all digits from the
above mentioned text file.
palindrome( ) -to store all palindrome words to a new file
“palindrome.txt”.
Read and display upper.txt, digit.txt and palindrome.txt.
def create():
f=open("sentence.txt","w")
n=int(input("Enter number of lines:"))
for i in range(n):
y=input("Enter line:")
f.write(y+"\n")
f.close()
def moveCharacters( ):
f=open("sentence.txt","r")
#to clear old data
a=open("lower.txt","w")
SOURCE b=open("upper.txt","w")
CODE: c=open("digit.txt","w")
a.close()
b.close()
c.close()
a= open('lower.txt','a')
b= open('uppper.txt','a')
c= open('digit.txt','a')
r= f.readlines()
f.close()
for i in r:
for j in i:
if j.islower():
a.write(j+ ' ')
elif j.isupper():
b.write(j+' ')
elif j.isdigit():
c.write(j+' ')
a.close()
b.close()
c.close()
def palindrome( ):
f = open('sentence.txt','r')
p = open('palindrome.txt','w')
w = f.read().split()
f.close()
#checking if a word is palindrome or not
for i in w:
if i == i[::-1]:
p.write(i+' ')
p.close()
def display():
print()
a= open('lower.txt','r')
b= open('uppper.txt','r')
c= open('digit.txt','r')
p = open('palindrome.txt','r')
print("Letters in lower.txt")
print("- - - - - - - - - - - ")
print(a.read())
print()
print("Letters in upper.txt")
print("- - - - - - - - - - - ")
print(b.read())
print()
print("Letters in digit.txt")
print("- - - - - - - - - - - ")
print(c.read())
print()
print("Letters in palindrome.txt")
print("- - - - - - - - - - - - ")
print(p.read())
print()
a.close()
b.close()
c.close()
p.close()
def main():
create()
moveCharacters()
palindrome()
display()
main()
Enter number of lines:5
Enter line:Class 12th D
Enter line:Hallelujah
Enter line:malayalam 123
Enter line:John jacobs
Enter line:king kong
Letters in lower.txt
-----------
lassthallelujahmalayalamohnjacobskingkong
Letters in upper.txt
-----------
CDHJ
OUTPUT: Letters in digit.txt
-----------
12123
Letters in palindrome.txt
------------
D malayalam