Signup/Sign In
LAST UPDATED: DECEMBER 10, 2020

NumPy splitlines() function

In this tutorial, we will cover splitlines() function of the char module in the Numpy library.

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 = "Studytonight \n is a best place \n to learn coding online"
out = np.char.splitlines(string1) 
print("After applying splitlines() function:")
print(out)


After applying splitlines() function:
['Studytonight ', ' is a best place ', ' to learn coding online']

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) 


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

Summary

In this tutorial we learnd about splitlines() function of the Numpy library which is used to split string present in array into substrings based on the line break character.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.