This is an important learning moment

1️⃣ Why blank lines appear Example file (example.txt) Hello World There is an empty line between Hello and World. Code with open("example.txt") as f: for line in f: print(line) Output Hello World Why? Each line in a file ends with \n. What Python actually reads: "Hello\n" "\n" "World\n" That middle line is just a newline, so when printed, it shows as a blank line. 2️⃣ How .strip() behaves .strip() removes: spaces tabs newlines (\n) Example text = " Hello World \n" print(text) print(text.strip()) Output Hello World Hello World Important case line = "\n" print(line.strip()) Output (empty line) 👉 Because "\n".strip() becomes "" (empty string) 3️⃣ Truthy / Falsy strings (VERY important) In Python, not all values are treated as True. Falsy values bool("") # False bool(0) # False bool(None) # False bool([]) # False Truthy values bool("hello") # True bool(" ") # True (space is still a character) bool("0") # True Example with strings if "": print("This will NOT run") if "AWS": print("This WILL run") Why this matters for files line = line.strip() if line: print(line) This means: “Only print the line if it’s not empty.” 4️⃣ How to filter input properly Filtering means: “Keep what I want, ignore the rest.” Example 1: Remove empty lines if line: print(line) Example 2: Remove a specific symbol if line != "|": print(line) Example 3: Combine filters (this is what you used ✅) if line and line != "|": print(line) Meaning: line → not empty line != "|" → not a separator Full working example with open("example.txt") as f: for line in f: line = line.strip() if line and line != "|": print(line) 🔑 Mental model (remember this) Files always contain newlines strip() cleans the edges Empty strings are False if line: is your best friend Mini challenge (optional but powerful 💪) What will this print? values = ["", " ", "AWS", "|"] for v in values: if v.strip(): print(v) If you can answer that, you’ve fully got it. Want to move next into: grouping records writing clean output files CSV/Excel real AWS account automation You’re building real skill here 👏

0 Comments