Python 3 Script to Capitalize First and Last Letters or Characters of Each Word in Text File Full Project For Beginners

Python 3 Script to Capitalize First and Last Letters or Characters of Each Word in Text File Full Project For Beginners

 

Welcome folks today in this blog post we will be capitalize first and last letters of each word in text file using python. All the full source code of application is given below.

 

 

Get Started

 

 

In order to get started we need to make an app.py file and copy paste the following code

 

app.py

 

def capitalize_first_last_letters(line):
     line = result = line.title()
     result =  ""
     for word in line.split():
        result += word[:-1] + word[-1].upper() + " "
     return result[:-1]  
     
print(capitalize_first_last_letters("python exercises practice solution"))
print(capitalize_first_last_letters("w3resource"))

file_gfg = open('file.txt', 'r') 
output=""
# This will traverse through every line one by one 
# in the file 
for line in file_gfg: 
    
    # This will convert the content 
    # of that line with capitalized 
    # first letter of every word 
    output+=capitalize_first_last_letters(line.title()) + "\n" 
    
    # This will print the output 
    print(output)

f = open("output.txt","w")

f.write(output)

 

See also  Python 3 Tkinter Webcam Video Recorder & Selfie Capture as PNG Image Using OpenCV & Pillow Library GUI Desktop App Full Project For Beginners

 

Now if you execute this python script by typing the below command as shown below

 

python app.py

 

 

 

Leave a Reply