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

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

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().

  • 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.

Key point

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().

File objectone line at a timelinetext from the fileupper()uppercase transformationprint()transformed linereadspasses textreturns transformed string
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.

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

Transforming Each Line

Code walkthrough
filename = input("Enter a filename: ")
file_object = open(filename)

for line in file_object:
    print(line.upper())

file_object.close()
  1. 1
    The input call obtains the filename that the user provides.
  2. 2
    The open call makes the file contents available through file_object.
  3. 3
    The for loop assigns one file line at a time to line.
  4. 4
    line.upper() returns the current line with lowercase letters converted to uppercase.
  5. 5
    print outputs the transformed line.
  6. 6
    After all lines have been processed, close releases the file resource.
  • Prompts the user for a filename.
  • Opens the named file.
  • Begins a loop that reads one line during each repetition.
  • Calls upper() on the current line and sends the returned string to print().
  • Closes the file after the loop completes.
Worked example

Tracing Three Input Lines

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

  1. 1
    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.

  2. 2
    Second iteration

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

  3. 3
    Third iteration

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

  4. 4
    After the loop

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

Result

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

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

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().

hello worldincludes newlineHELLO WORLDnewline remainsupper()
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

Use upper() for the demonstrated uppercase file-processing task. The supplied material does not provide enough detail to specify the operations of lower() or strip().

Debugging the Transformation

  • Common mistake

    Applying upper() outside the loop

    What it looks like

    The program must transform each current line as the loop processes it.

    Why it fails

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

    Do this instead
    Use print(line.upper()) in the loop body.
  • Common mistake

    Forgetting to close the file

    What it looks like

    The loop finishes, but the program never calls close().

    Why it fails

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

    Do this instead

    Call file_object.close() after the loop completes.

  • Common mistake

    Assuming upper() changes punctuation or numbers

    What it looks like

    An email address is expected to lose its dots or change its other symbols.

    Why it fails

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

    Do this instead

    Compare only the letters when checking the transformation.

  • Common mistake

    Ignoring the newline while tracing output

    What it looks like

    The line is treated as only hello world rather than hello world followed by a newline.

    Why it fails

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

    Do this instead

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

At a glance

  • 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.

More concepts starting with S