Write a program to capitalize first letter of every word in a file in Python

First, read each line from the file and then use the title() function to capitalize each word in the line.

string.title()

Example program to capitalize the first letter of every word in a file in Python

Simple example code takes the content of the file as input and Prints it after altering it.

fname = "file.txt"

with open(fname, 'r') as f:
    for line in f:
        l = line.title()
        print(l)

Output:

Write a program to capitalize first letter of every word in a file in Python

Inside File.txt

Python Program to Read a File and Capitalize the First Letter

Do comment if you have any comments or suggestions on this Python capitalize program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.