You are here : python_2stringstringsplitlines

string.splitlines() - string

             

The method splitlines() returns a list with all the lines in string, optionally including the line breaks (if num is supplied and is true)


  • num -- This is any number, if present then it would be assumed that line breaks need to be included in the lines.


Syntax

str.splitlines( num=string.count('
'))


Example

#!/usr/bin/python

str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d";
print str.splitlines( )
print str.splitlines( 0 )
print str.splitlines( 3 )
print str.splitlines( 4 )
print str.splitlines( 5 )


Output / Return Value

When we run above program, it produces following result −


['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f ', 'Line2- a b c ', ' ', 'Line4- a b c d'] ['Line1-a b c d e f ', 'Line2- a b c ', ' ', 'Line4- a b c d'] ['Line1-a b c d e f ', 'Line2- a b c ', ' ', 'Line4- a b c d']


Limitations


Alternatives / See Also


Reference