A newline separates one line of text from the next. In Python, file iteration returns each line together with its trailing "\n"; even a blank line contains that single character. section 5.2 section 5.12
Line endings differ by operating system: Unix uses LF, Windows uses CR+LF, and older Macs used CR. Python normally translates these forms when reading text and chooses the platform form when writing. section 5.21
Handle newlines deliberately when processing files:
- Strip a line’s ending before transforming its content, then add a newline when writing the transformed line. section 5.31 section 5.32
- Remember that newlines count as characters and act as whitespace for split() without arguments. section 5.12
- Recognize a blank line with startswith('\n') when filtering records, alongside other unwanted lines such as comments. section 5.7
- Specify open(..., newline='\n') or newline='\r\n' when automatic line-ending handling produces unexpected results. section 5.21
Reverse each line without reversing its separator
with open(input_name) as source, open(output_name, 'w') as destination:\n for line in source:\n destination.write(line.rstrip()[::-1] + '\n')\n\nThe content is reversed while the newline remains at the end, so each output record stays on its own line. section 5.32 section 5.31
if not line.startswith(('#', '\n')):
Inspect a compact blank-line and comment filter that relies on every ordinary line carrying a newline character. section 5.7





