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 –  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"

# splitlines with carriage return
continents ="Asia\r Africa\r Europe\r NA\r SA\r Australia\r"

# splitlines with both newline and carriage return
countries="Russia\n UK\r USA\r France\n India\n"

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


\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


  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.