![]() |
VOOZH | about |
In this article, we will learn how to change the case of all characters present in a text file using Python. We will use Python methods Python upper() to change all characters to upper case, and Python lower() to change all characters to lower case.
For example, If we have text file data.txt as shown below.
In this method, we will read line by line from the text file and change the case, and write that line to a new text file. We will read each line from this file, change the case and write it into an output.txt file.
Output:
data.txt in read mode and output.txt in append mode. This allows reading data from the source file and adding content to the output file without overwriting it.data.txt, the program reads the content and processes it by converting the text to uppercase using the upper() method.output.txt using output_file.write(). In this method, we will read the entire file, convert the case of all characters and write it into the new file. Here also we will take the same txt file data.txt and read the entire file and convert characters to lowercase using the lower() method and write to the output.txt file.
Output:
Explanation:
data.txt in read mode and output.txt in append mode, allowing it to read the content from the source file and append the modified content to the output file without overwriting existing data.data_file.read() method reads the entire content of data.txt as a single string, and the lower() method converts all the text to lowercase.output.txt using output_file.write(), appending the modified content to the file.We will use an inbuilt method to capitalize the first letter of each word in data.txt. By looping through each word, we will modify the case of the first letter and write the changes to an output file.
Output:
data.txt is opened for reading and output.txt for appending.output.txt