Skip to content

Latest commit

 

History

History
22 lines (15 loc) · 601 Bytes

20220907130613.md

File metadata and controls

22 lines (15 loc) · 601 Bytes

splitlines

Using split("\n") to split a text into lines can return odd results if you are on Windows or an (older) Apple computer which use \r\n and \r for newlines respectively.

In those instances, splitlines() has you covered:

>>> "first line\r\nsecond line".split("\n")
['first line\r', 'second line']

# includes \r (Carriage Return) in the splitting
>>> "first line\r\nsecond line".splitlines()
['first line', 'second line']

>>> "first line\rsecond line".splitlines()
['first line', 'second line']

>>> "first line\nsecond line".splitlines()
['first line', 'second line']

#strings