The splitlines() Function in python splits the string at line breaks and returns a list of lines in the string.
Syntax of splitlines() Function in Python:
str.splitlines()
str – is the string object
Example of splitlines() Function in Python:
# splitlines with new lines solar="mercury\n venus\n earth\n mars\n Jupiter\n" solar.splitlines() # splitlines with carriage return continents ="Asia\r Africa\r Europe\r NA\r SA\r Australia\r" continents.splitlines() # splitlines with both newline and carriage return countries="Russia\n UK\r USA\r France\n India\n" countries.splitlines()
The splitlines() splits the line on \n and \r boundaries in above example. So the output will be
[‘mercury’, ‘ venus’, ‘ earth’, ‘ mars’, ‘ Jupiter’]
[‘Asia’, ‘ Africa’, ‘ Europe’, ‘ NA’, ‘ SA’, ‘ Australia’]
[‘Russia’, ‘ UK’, ‘ USA’, ‘ France’, ‘ India’]
[‘Asia’, ‘ Africa’, ‘ Europe’, ‘ NA’, ‘ SA’, ‘ Australia’]
[‘Russia’, ‘ UK’, ‘ USA’, ‘ France’, ‘ India’]
The splitline() Function splits into lines on below mentioned separators
Notation | Description |
---|---|
\n | Line Feed |
\r | Carriage Return |
\r\n | Carriage Return + Line Feed |
\v or \x0b | Line Tabulation |
\f or \x0c | Form Feed |
\x1c | File Separator |
\x1d | Group Separator |
\x1e | Record Separator |
\x85 | Next Line (C1 Control Code) |
\u2028 | Line Separator |
\u2029 | Paragraph Separator |