Splitlines() Method Of Str Class

Method Name:

splitlines

 

Method Signature:

splitlines()

 

Method Overview:

  • splitlines() of Python str class, breaks a string into multiple strings based on a set of line separators and returns the result in a python list object

 

  • The line separator characters include

 

Line Separator

Description

\n

Line Feed

\r

Carriage Return

\r\n

Carriage Return and Line Feed

\v or \x0b

Line Tabulation, Vertical Tab

\f or \x0c

Form Feed

\x1c

File Separator

\x1d

Group Separator

\x1e

Record Separator

\x85

Next Line

\u2028

Line Separator

\u2029

Paragraph Separator

 

  • Together with split() and splitlines()can be used to tokenize entire files of data which can be fed into relational data store or any other NoSQL data stores.

Example:

sampleText = """The Little Prince is

a classic tale of equal appeal

to children and grown ups."""

lines           = sampleText.splitlines()            

 

print(lines)

 

linesWithEnds   = sampleText.splitlines(keepends=True)

print(linesWithEnds)

 

Output:

['The Little Prince is', 'a classic tale of equal appeal ', 'to children and grown ups.']

['The Little Prince is\n', 'a classic tale of equal appeal \n', 'to children and grown ups.']


Copyright 2023 © pythontic.com