prize18-7 Exercise 2: Line Endings =================================== Predictions: normalise_newlines gives 'one\ntwo\nthree\nfour'. splitlines also splits at all three (and at some other separators), giving four items. With newline set to LF the bytes are a\nb\n; with CR LF they are a\r\nb\r\n; with the default (None) Python writes the platform's line ending, so on Windows it matches CR LF and on Linux or macOS it matches LF. That last line is the machine-dependent one. ex2.py: import tempfile, pathlib def normalise_newlines(text): return text.replace("\r\n", "\n").replace("\r", "\n") mixed = "one\r\ntwo\rthree\nfour" print(repr(normalise_newlines(mixed))) print(mixed.splitlines()) d = pathlib.Path(tempfile.mkdtemp()) p = d / "x.txt" for nl in ["\n", "\r\n", None]: with open(p, "w", encoding="utf-8", newline=nl) as f: f.write("a\nb\n") print(repr(nl), p.read_bytes()) Real output (Python 3.14, on Windows): 'one\ntwo\nthree\nfour' ['one', 'two', 'three', 'four'] '\n' b'a\nb\n' '\r\n' b'a\r\nb\r\n' None b'a\r\nb\r\n' Order matters in normalise_newlines: replace CR LF first, otherwise the lone-CR rule would turn each CR LF into two line feeds. Note that splitlines is more aggressive than it looks: it also splits at some other Unicode line separators (check the list in the Python documentation).