Files in Python:
Python capitalize first letter of every word: Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).
We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).
We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.
As a result, a file operation in Python is performed in the following order:
- Create a new file
- You can either read or write (perform the operations)
- Close the file.
title() method in Python:
Python capitalize first letter of every word in string: To capitalize the initial letter, we use Python’s title() method. The Python String Method is the title function in Python, and it is used to change the initial character in each word in the string to Uppercase and the remaining letters in the string to Lowercase, and it returns the new string.
Given a file, the task is to capitalize the first letter of every word in the given file in python
- Python Program to Split a String at First Instance of a Character
- Python Program to Reverse Each Word in a Text File
- Python Program to Delete Specific Line from a Text File
Program to Capitalize the First Letter of Every Word in the File in Python
Below is the full approach to capitalize the first letter of every word in the given file in python
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read-only mode. In this case, we’re simply just reading the contents of the file.
- Iterate in the above file lines using the for a loop.
- Capitalize the first letter of each word in a line using the title() function and store it in a variable.
- Print the lines in a file after capitalizing the first letter of each word in a line.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read-only mode. In this case, we're simply just reading the contents of the file. gvn_file = open(givenFilename, 'r') # Iterate in the above file lines using the for loop for fline in gvn_file: # Capitalize the first letter of each Word in a line using the title() function # and store it in a variable rslt = fline.title() # Print the lines in a file after capitalizing the first letter of each Word in a line. print(rslt)
Output:
Hello This Is Btechgeeks Welcome To Btechgeeks Good Morning This Is Btechgeeks
File Content:
hello this is btechgeeks welcome to btechgeeks good morning this is btechgeeks
Google Colab Images:
Files and Code: