splitlines() Function in Python

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’]

 

The splitline() Function splits into lines on below mentioned separators

 

NotationDescription
\nLine Feed
\rCarriage Return
\r\nCarriage Return + Line Feed
\v  or  \x0bLine Tabulation
\f  or  \x0cForm Feed
\x1cFile Separator
\x1dGroup Separator
\x1eRecord Separator
\x85Next Line (C1 Control Code)
\u2028Line Separator
\u2029Paragraph Separator

previous splitlines() Function in Python                                                                                                                next splitlines() Function in Python