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().
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().
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.
Transforming Each Line
filename = input("Enter a filename: ")
file_object = open(filename)
for line in file_object:
print(line.upper())
file_object.close()- 1The input call obtains the filename that the user provides.
- 2The open call makes the file contents available through
file_object. - 3The for loop assigns one file line at a time to line.
- 4
line.upper()returns the current line with lowercase letters converted to uppercase. - 5print outputs the transformed line.
- 6After 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 toprint(). - Closes the file after the loop completes.
Tracing Three Input Lines
Imagine that the file contains the three lines hello world, python rocks, and file i/o.
- 1First iteration
The loop reads the first line as hello world followed by a newline character.
upper()returns HELLO WORLD followed by that newline, andprint()outputs the transformed string. - 2Second iteration
The loop reads python rocks.
upper()transforms the letters to PYTHON ROCKS, while the line's other characters remain as they are. - 3Third iteration
The loop reads file i/o.
upper()transforms the letters to FILE I/O, while the slash remains unchanged. - 4After 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.
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().
| Method | Role supported by the source pack | Demonstrated use |
|---|---|---|
| upper() | Transforms lowercase letters to uppercase | line.upper() |
| lower() | Named as a related string method; detailed behavior is not specified in the supplied material | No demonstrated use |
| strip() | Named as a related string method; detailed behavior is not specified in the supplied material | No 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 likeThe program must transform each current line as the loop processes it.
Why it failsThe file object supplies lines one at a time, so the transformation belongs with the current line inside the loop.
Do this insteadUse print(line.upper()) in the loop body.Common mistake Forgetting to close the file
What it looks likeThe loop finishes, but the program never calls close().
Why it failsThe source identifies closing the file as a best practice that releases system resources.
Do this insteadCall file_object.close() after the loop completes.
Common mistake Assuming upper() changes punctuation or numbers
What it looks likeAn email address is expected to lose its dots or change its other symbols.
Why it failsupper()changes letters, while punctuation, spaces, digits, and special characters remain unchanged.Do this insteadCompare only the letters when checking the transformation.
Common mistake Ignoring the newline while tracing output
What it looks likeThe line is treated as only hello world rather than hello world followed by a newline.
Why it failsA file line can include its newline character, and
upper()leaves that character in the returned string.Do this insteadUse repr(line) when tracing so hidden newline characters are visible.
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
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.
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()torepr()for the transformed representation.
Essential Takeaways
- 1A for loop over a file object reads one line during each iteration and stops after all lines have been read.
- 2Calling
line.upper()returns a version of the current line with lowercase letters converted to uppercase. - 3Digits, punctuation, spaces, special characters, and the demonstrated newline remain unchanged by
upper(). - 4The 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.
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.