Concepts / String Methods: upper(), lower(), and strip()

String Methods: upper(), lower(), and strip()

Write a program that prompts the user for a filename, opens the file, and prints each line in uppercase using the upper() string method.

  • Programming

A File-to-Text Task

The shout.py exercise combines three file-handling skills: opening a file, processing its contents line by line, and transforming the text before printing it. The program asks the user for a filename, opens that file, uses a for loop to read each line, converts each line to uppercase with upper(), prints the result, and closes the file.

The important transformation happens inside the loop. Each line is read first, upper() is then called on that line, and the transformed string is passed to print().

readspasses textreturns transformed stringFile objectone line at a timelinetext from the fileupper()uppercase transformationprint()transformed line
How does a line move from the file through the loop and become uppercase before it is printed?

Reading One Line at a Time

A file object can be used directly in a for loop. In each repetition, Python automatically reads one line from the file object and assigns that line to the loop variable. The loop continues until all lines have been read. This means the program processes the file one line at a time instead of loading the entire file into memory at once.

readsnext iterationchecksyesnoFile objectready to readFirst lineline = first lineNext lineline = next lineLoop repeatsmore lines availableLoop endsall lines read
How does the file object provide one line at a time to the loop, and when does the loop stop?

Transforming Each Line

filename = input("Enter a filename: ") file_object = open(filename) for line in file_object: print(line.upper()) file_object.close()

Tracing Three Input Lines

Imagine that the file contains the three lines hello world, python rocks, and file i/o.

First iteration: The loop reads the first line as hello world followed by a newline character. upper() returns HELLO WORLD followed by that newline, and print() outputs the transformed string.

Second iteration: The loop reads python rocks. upper() transforms the letters to PYTHON ROCKS, while the line's other characters remain as they are.

Third iteration: The loop reads file i/o. upper() transforms the letters to FILE I/O, while the slash remains unchanged.

After the loop: When all lines have been read, the loop stops and the program closes the file.

The program prints the file contents in uppercase while preserving the original line structure and non-letter characters.

upper()print()hello worldbefore upper()HELLO WORLDafter upper()Printed linetransformed string
What changes in a line when upper() is applied, and where does the transformed string go?

The upper() string method returns a new string with lowercase letters converted to uppercase. Digits, punctuation, spaces, and special characters remain unchanged. For example, stephen.marquard@uct.ac.za becomes STEPHEN.MARQUARD@UCT.AC.ZA.

Line Endings and Named Methods

A line read from the file can include its newline character. In the source tracing example, the first line is represented as hello world followed by a newline, and upper() changes the letters while leaving that newline in the string. The supplied material names lower() and strip() as related string methods, but it does not define their detailed behavior or include them in the demonstrated file-processing program. The fully supported transformation in this exercise is upper().

upper()hello worldincludes newlineHELLO WORLDnewline remains
What remains in a line after upper() changes its letters?
MethodRole supported by the source packDemonstrated use
upper()Transforms lowercase letters to uppercaseline.upper()
lower()Named as a related string method; detailed behavior is not specified in the supplied materialNo demonstrated use
strip()Named as a related string method; detailed behavior is not specified in the supplied materialNo demonstrated use

Debugging the Transformation

  • Applying upper() outside the loop

    The file object supplies lines one at a time, so the transformation belongs with the current line inside the loop.

    Fix: Use print(line.upper()) in the loop body.

  • Forgetting to close the file

    The source identifies closing the file as a best practice that releases system resources.

    Fix: Call file_object.close() after the loop completes.

  • Assuming upper() changes punctuation or numbers

    upper() changes letters, while punctuation, spaces, digits, and special characters remain unchanged.

    Fix: Compare only the letters when checking the transformation.

  • Ignoring the newline while tracing output

    A file line can include its newline character, and upper() leaves that character in the returned string.

    Fix: Use repr(line) when tracing so hidden newline characters are visible.

python

The repr() function helps reveal the exact string representation, including a hidden newline character. Comparing the Before and After values lets you locate the transformation: the letters change when upper() is called, while the newline remains visible in the representation.

Practice the Trace

MEDIUM

Trace the program for a file containing the lines Good morning, Python, and 123 apples. For each iteration, write the value assigned to line, the result of line.upper(), and the characters that remain unchanged.

Hints
  • The for loop supplies one line during each iteration.
  • upper() changes lowercase letters to uppercase.
  • Punctuation, spaces, digits, and special characters remain unchanged.
  • Remember that a line may include its newline character.
EASY

Add the Before and After repr() tracing statements inside the loop. Use the displayed representations to identify exactly which characters changed after upper() was called.

Hints
  • Place the statements inside the loop so they run for each line.
  • Pass line to repr() for the original representation.
  • Pass line.upper() to repr() for the transformed representation.

Essential Takeaways

  1. A for loop over a file object reads one line during each iteration and stops after all lines have been read.
  2. Calling line.upper() returns a version of the current line with lowercase letters converted to uppercase.
  3. Digits, punctuation, spaces, special characters, and the demonstrated newline remain unchanged by upper().
  4. The complete file-processing sequence is prompt, open, loop, transform, print, and close.
  5. repr() is useful for tracing hidden newline characters and comparing a line before and after transformation.

Key Takeaways

  • Use a for loop to process a file one line at a time.
  • Apply upper() inside the loop to transform each current line.
  • upper() changes letters but preserves the other characters described in the source, including a line's newline character.
  • Close the file after processing is complete.
  • Use repr() to trace exact line contents during debugging.