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.
In order to get started we need to make an
app.py file and copy paste the following code
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)
Now if you execute this
python script by typing the below command as shown below