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.
The syntax required to use this function is as follows:
The above syntax indicates that
splitlines() function takes two parameters.
let us discuss the above-given parameters of this function and these are as follows:
This parameter represents the input array of strings.
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.
This function will return the array of List objects.
Example 1: Without
The code snippet is as follows where we will use
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
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.']
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.