What is the numpy.char.splitlines() function in Python?

Overview

The char.splitlines() function in Python is used to return a list of lines in the element of an input array of strings or Unicode, breaking at line boundaries.

Syntax

char.splitlines(a, keepends=None)

Parameter

The char.splitlines() function takes the following parameter values:

  • a: The input array of strings or Unicode.
  • keepends: Takes a boolean value whether line breaks are to be included in the resulting list or not.

Example

Let’s look at the code below:

import numpy as np
# implementing the char.splitlines() function using keepends as True
print(np.char.splitlines("I love \nnumpy \nso very \nmuch", keepends= True))
# using keepends as False
print(np.char.splitlines("I love \nnumpy \nso very \nmuch", keepends= False))

Explanation

In the code above:

  • Line 1: We import the numpy module.
  • Line 4: We implement the char.splitlines() function on an array of strings using the keepeends value as True. We obtain and print our results to the console.
  • Line 7: We implement the char.splitlines() function on an array of strings using the keepeends value as False. We obtain and print our results to the console.

Free Resources