Skip to content

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

  • by

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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Your email address will not be published. Required fields are marked *