We revisit opening, reading, and writing files โ then meet our first string functions: upper() and lower(). All code is provided โ copy it, run it, and experiment!
for loop.upper() to convert a string to ALL CAPITALS.lower() to convert a string to all lowercaseClick a term on the left, then click its matching definition on the right.
input.txt. Save it in the same folder where you will save your Python file. Copy the text below and paste it into Notepad, then save the file:
My Name Is Alex i love coding in python GCSE Computer Science Is Fun hello World The Quick Brown Fox jersey is a BEAUTIFUL island
lesson1.py in the same folder as your input.txt. Run it with F5. Check the output.txt file that is created!
# ============================================ # Lesson 1 โ File Recap & String Case # ============================================ # --- STEP 1: Open the input file for reading --- input_file = open("input.txt", "r") # --- STEP 2: Open the output file for writing --- output_file = open("output.txt", "w") # --- STEP 3: Loop through every line in the input file --- for line in input_file: # Remove the newline character from the end of each line line = line.strip() # Convert the line to UPPER CASE upper_line = line.upper() # Convert the line to lower case lower_line = line.lower() # Print both versions to the screen print("UPPER:", upper_line) print("lower:", lower_line) # Write the upper case version to the output file output_file.write(upper_line + "\n") # --- STEP 4: Close both files --- input_file.close() output_file.close() print("\nโ Done! Check your output.txt file.")
print("---") after the two print statements so each pair is separated by a line.line.UPPER() instead of line.upper()? Try it and note the error!title_line using Python's .title() โ what does it do?Complete the code below by typing in the missing parts. Press Check when done.
"Hello World".upper() return?name = "Python", what is the value of name after name.lower()?.strip() when reading file lines?Look at each code snippet and predict what will be printed. Click to reveal the answer!
word = "Python"
print(word.upper())
print(word)text = "Hello World"
result = text.lower()
print(result)name = "jersey"
name.upper()
print(name)Match each function to its description. Click two cards to flip them!
.title() method to convert to title case.swapcase() โ what does it do? Find out!