
"Let's say we have some text that was retrieved from a database, and the original text came from a form submission in a web browser. Web browsers often represent line breaks as a carriage return character, followed by a line feed character: That's what we see in our text as well: \r followed by \n. This is often called CRLF (carriage return and line feed) whereas \n is called LF (line feed)."
"If we use the string split method to split by \n, we would see that each line ends in a carriage return character: That's kind of confusing! If your application reads text from different sources and you don't deliberately normalize your line endings, you may end up with a mix of different line endings for different types of text. It's unfortunately common for some text in an application to use \n line endings, while other text uses \r\n line endings."
The splitlines method exists because different sources use different newline conventions and splitting on '\n' alone can leave stray carriage return characters. Web browsers often submit line breaks as CRLF (\r\n) while other sources use LF (\n), producing mixed line endings across an application. Splitting on '\n' can yield confusing results when CRLF appears. The splitlines method recognizes and splits on all common newline sequences, preventing embedded '\r' characters in resulting lines. Trailing newlines and normalization across sources remain important considerations when processing line-oriented text.
Read at Pythonmorsels
Unable to calculate read time
Collection
[
|
...
]