Copyright © tutorialspoint.com

Python String splitlines() Method

previous next


Description:

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

Syntax:

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

Parameters:

Here is the detail of parameters:

Return Value:

It returns true if found matching string otherwise false.

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 );

This will produce following result, note each line indicates an array:

['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\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']

previous next

Copyright © tutorialspoint.com