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

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

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

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)
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment