Change language

How do I use the String splitlines () method in Python?

| |

Today in this tutorial we will discuss the Python string splitlines () method. First, let’s take a look at the main method definition.

 define Splitlines ()

What is the String splitlines () method in Python?

Python string splitlines () &it is a built-in method that returns a list of strings in a string, with a break at the line boundaries. Line breaks are not included in the resulting list unless keepends is specified as true.



Syntax.

 str.splitlines ([keepends]) 

where,

  • str &the string object that we need to split into a list of strings,
  • keepends, when True is set, the line boundaries are included in the resulting list items. Otherwise, no line breaks are included.

A table for line break characters and their corresponding descriptions are shown below.

Line break table

character Python representation
n Line feed
p Carriage return
rn Carriage return + line feed
v or x0b Tabbing strings (Python 3.2 and later)
f or x0c Form Submission (Python 3.2 and later)
x1c File Separator
x1d Group Separator
x1e Record Separator
x85 Next line (C1 check code)
u2028 Line Separator
u2029 Paragraph separator

No leftovers

As mentioned before, without mentioning the parameter keepends will generate a list of split lines excluding line breaks or boundary characters. 

Take a look at the example below.



 # String initialisation string1 =" Tim Charlie John Alan "string2 =" Welcome to AskPython! " string3 = "Keyboardu2028Monitoru2029x1cMousex0cCPUx85Motherboardx1eSpeakers UPS" #without keepends print (string1.splitlines ()) print (string2.splitlines ()) print (string3.splitlines ()) 

Output:

 [’Tim’,’ Charlie’, ’John’,’ Alan’] [’Welcome’,’ ’,’ to’, ’AskPython!’] [’Keyboard ’,’ Monitor’, ’’, ’Mouse’,’ CPU’, ’Motherboard’,’ Speakers’, ’UPS’] 

where,

  • We have announced three lines containing different words, separated by different line breaks,
  • We pass each one to the inline splitlines () with the default keepends (false). And we print the resulting lists of split lines.

As you can see from the output, since keepends was not installed, all split lines have no line boundaries or boundary characters. For line 2, ’’ is included in the word ’Askpython’ because it is not a boundary character (it is not in the table).

So the output is justified.

With matching line feeds included

If we specify keepends as True , the split lines will now include matching line feeds.

Let’s modify our previous code (without keepends) by setting the keepends parameter to True inside the splitlines () string method. Take a close look at the result and try to notice the difference from the previous one.

 # String initialization string1 = "Tim Charlie John Alan" string2 = "Welcome to AskPython! " string3 = "Keyboardu2028Monitoru2029x1cMousex0cCPUx85Motherboardx1eSpeakers UPS" #with keepends print (string1.splitlines (keepends = True)) print (string2.splitlines (keepends = True)) print (string3.splitlines (keepends = Truep)) : 

 [’Tim’, ’Charlie’, ’John’, ’Alan’] [’ Welcome ’,’ ’,’ to ’ , ’AskPython!’] [’Keyboardu2028’,’ Monitoru2029’, ’x1c’,’ Mousex0c’, ’CPUx85’,’ Motherboardx1e’, ’Speakers’,’ UPS’] 

As expected, for the same lines, splitlines () includes all boundary characters.



Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically