NumPy splitlines() function

For each element in an array the splitlines() function is used to return a list of the lines in the element, breaking at line boundaries.

This function calls the str.splitlines for every element in the given array.

Syntax of splitlines():

The syntax required to use this function is as follows:

numpy.char.splitlines(a, keepends=None)

The above syntax indicates that splitlines() function takes two parameters.

Parameters:

let us discuss the above-given parameters of this function and these are as follows:

  • a This parameter represents the input array of strings.
  • keepends This is an optional argument having boolean values. If we want to include line breaks, then we can use this parameter, by setting its value as True.

Returned Values:

This function will return the array of List objects.

Example 1: Without keepends parameter

The code snippet is as follows where we will use splitlines() function:

import numpy as np

string1 = "Shishir Kant Singh\n is a best teacher \n to learn programming coding"
out = np.char.splitlines(string1) 
print("After applying splitlines() function:")
print(out)

Output:

After applying splitlines() function:
['Shishir Kant Singh ', ' is a best teacher ', ' to learn programming coding']

Example 2: With keepends parameter

import numpy as np

x = np.char.splitlines('The quick brown \rfox jumps over \rthe lazy dog.', keepends=False)
print(x) 

Output:

['The quick brown ', 'fox jumps over ', 'the lazy dog.']
Follow Us On