Open In App

Change case of all characters in a .txt file using Python

Last Updated : 16 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

 

Change all characters to uppercase from the text file

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.

Python3




with open('data.txt', 'r') as data_file:
     
    # open output.txt file in append mode
    with open('output.txt', 'a') as output_file:
         
        # read each line from data.txt
        for line in data_file:
             
            # change case for the line and write
            # it into output file
            output_file.write(line.upper())


Output:

Change all characters to uppercase from the text file

 

Change all characters to lowercase from the text file

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.

Python3




with open('data.txt', 'r') as data_file:
     
    # open output.txt file in append mode
    with open('output.txt', 'a') as output_file:
         
        # read data.txt file, convert case,
        # and write to output.txt file
        output_file.write(data_file.read().lower())


Output:

Change all characters to lowercase from the text file

 

Capitalize the first letter of each word from the text file

The first letter of each word found in the text file will be capitalized in this manner. The input file will be the same data.txt file. We need an inbuilt technique to simply change the first letter to uppercase. Therefore, using a new function, we will loop through each word in the file and change the case of the word’s first letter.

Python3




def capitalize_first_letter(wrd):
    # convert first letter to upper and
    # append the rest of the string to it
    return wrd[0].upper() + wrd[1:].lower()
 
# open data.txt file in reading mode
with open('data.txt', 'r') as data_file:
   
    # open output.txt file in append mode
    with open('output.txt', 'a') as output_file:
       
        # traverse each line in data.txt file
        for line in data_file:
           
            # split line into words
            word_list = line.split()
             
            # apply function capitalize_first_letter
            # on each word in current line
            word_list = [capitalize_first_letter(word) for word in word_list]
             
            # write the line into output.txt file after
            # capitalizing the first letter in a word
            # in the current line
            output_file.write(" ".join(word_list) + "\n")


Output:

Capitalize the first letter of each word from the text file

 



Previous Article
Next Article

Similar Reads

Install Packages Using PIP With requirements.txt File in Python
Installing more than one package in Python simultaneously is a common requirement for any user migrating between operating systems. Most users are unaware of the availability of batch installing all the packages. They default to manually installing each library one after the other, which is time-consuming. This article will teach you how to install
2 min read
Convert PDF to TXT File Using Python
As the modern world gets digitalized, it is more and more necessary to extract text from PDF documents for purposes such as data analysis or content processing. There is a versatile ecosystem of Python libraries that can work with different file formats including PDFs. In this article, we will show how to build a simple PDF-to-text converter in Pyt
2 min read
Find line number of a specific string or substring or word from a .txt file in Python
Finding the line number of a specific string and its substring is a common operation performed by text editors or any application with some level of text processing capabilities. In this article, you will learn how to find line number of a specific string or substring or word from a .txt (plain text) file using Python. The problem in hand could be
4 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
How to Create Requirements.txt File in Python
Creating and maintaining a requirements.txt file is a fundamental best practice for Python development. It ensures that your project's dependencies are well-documented and easily reproducible, making it easier for others to work on your code and reducing the likelihood of compatibility issues. Create Requirements.txt File in PythonWhen working on P
2 min read
Print the Content of a Txt File in Python
Python provides a straightforward way to read and print the contents of a .txt file. Whether you are a beginner or an experienced developer, understanding how to work with file operations in Python is essential. In this article, we will explore some simple code examples to help you print the content of a .txt file using Python. How To Print The Con
3 min read
Find all the Files in a Directory with .txt Extension in Python
Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory with a .txt (text files) extension using Python.
5 min read
Convert TSV to TXT in Python
In this article, we are going to see how to convert TSV files to text files in Python. Approach:Open TSV file using open() functionOpen txt file in which we are going to write TSV file dataThen use csv.reader() it will return a reader object which will iterate over lines in the given TSV file. (set delimiter="\t")Write data in the opened txt file l
2 min read
How to install Python packages with requirements.txt
Python package is a container for storing multiple Python modules. We can install packages in Python using the pip package manager. In this article, we will learn to install multiple packages at once using the requirements.txt file. We will use the pip install requirements.txt command to install Python packages. The requirements.txt file is useful
1 min read
Python regex to find sequences of one upper case letter followed by lower case letters
Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print 'Yes', otherwise 'No'. Examples: Input : GeeksOutput : YesInput : geeksforgeeksOutput : NoPython regex to find sequences of one upper case letter followed by lower case lettersUsing re.search() To check if the sequence of one upper case
2 min read