How to Change Case of All Characters in a .txt File in Python

lower() Method:

The lower() method in python returns a string with all characters in lower case. Symbols and numbers are ignored by this method.

upper() Method:

The upper() method in python returns a string with all characters in the upper case. Symbols and numbers are ignored by this method.

Let us take a samplefile.txt as an Example here which contains the following content.

samplefile.txt:

Hello THIS is PYTHON-programs PLATform
GOOd morning All

Converting All Characters in a Text File to Uppercase

Approach:

  • Open some random file in read-only mode by passing filename/path, mode as arguments to it.
  • Read the given file using the read() function and loop through each letter of the text file using the for loop
  • Convert each letter of the given text file to uppercase using the upper() function and store it in a variable.
  • Open some random new file in append mode using the open() function by passing file path, mode as arguments to it.
  • Store it in another variable.
  • Append the above converted uppercase character to the new file using the write() function.
  • The Exit of the Program.

Below is the implementation:

# Open some random file in read-only mode by passing filename/path, mode as arguments to it. 
gvn_file = open('samplefile.txt', 'r')
# Read the given file using read() function and loop through each letter of the text file
# using the for loop
for k in gvn_file.read():
    # Convert each letter of the given text file to uppercase using the upper() function
    # and store it in a variable.
    res = k.upper()
    # Open some random new file in append mode using the open() function by passing file path, 
    # mode as arguments to it. 
    # Store it in another variable.
    output_file = open('output_file', 'a')
    # Append the above converted uppercase character to the new file using the write() function
    output_file.write(res)

Output:

HELLO THIS IS PYTHON-PROGRAMS PLATFORM
GOOD MORNING ALL

Converting All Characters in a Text File to Lowercase

Approach:

  • Open some random file in read-only mode by passing filename/path, mode as arguments to it.
  • Read the given file using the read() function and loop through each letter of the text file using the for loop
  • Convert each letter of the given text file to lowercase using the lower() function and store it in a variable.
  • Open some random new file in append mode using the open() function by passing file path, mode as arguments to it.
  • Store it in another variable.
  • Append the above converted lowercase character to the new file using the write() function.
  • The Exit of the Program.

Below is the implementation:

# Open some random file in read-only mode by passing filename/path, mode as arguments to it. 
gvn_file = open('samplefile.txt', 'r')
# Read the given file using read() function and loop through each letter of the text file
# using the for loop
for k in gvn_file.read():
    # Convert each letter of the given text file to lowercase using the lower() function
    # and store it in a variable.
    res = k.lower()
    # Open some random new file in append mode using the open() function by by passing file path, 
    # mode as arguments to it. 
    # Store it in another variable.
    output_file = open('output_file', 'a')
    # Append the above converted lowercase character to the new file using the write() function
    output_file.write(res)

Output:

hello this is python-programs platform
good morning all