NumPy split() function

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

The split() function is used to return a list of strings after breaking the input string by the specified separator(sep).

This function usually calls str.split internally on every element of the array.

Syntax of numpy.char.split():

The syntax required to use this function is as follows:

numpy.char.split(a, sep=None, maxsplit=None)

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


Let us discuss the parameters of this function:

  • a
    This parameter indicates the input string or the input array of strings

  • sep
    It can be string or Unicode(single character). If sep is not specified or it is None then by default the whitespace string is used as a separator.

  • maxsplit
    It is an integer and If it is given, at most maxsplit splits are done.

Returned Values:

This function will return an array of the list of splitted string values as an output.

Example 1: With a simple string

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

import numpy as np

a = "How you doing?"
print("The input is :\n",a)

y = np.char.split(a)
print("after applying split() method output is:\n",y)

The input is :
How you doing?
after applying split() method output is:
['How', 'you', 'doing?']

Example 2: With maxsplit Parameter

In the example below, we will use the maxsplit parameter of the split() function. In the below code, we will try to split the given string based on comma(,) and you will see although there are 2 commas in the complete string, the string will be split only once, because we have provided the maxsplit parameter value as 1.

import numpy as np

x = "We, very happily, welcome you all!"
print("The String is:\n", x)

out = np.char.split(x, sep=',', maxsplit=1)
print("The output is:", out)

The String is:
We, very happily, welcome you all!
The output is: ['We', ' very happily, welcome you all!']

If we do not provide the maxsplit parameter, then the output would be: ['We',' very happily',' welcome you all!']

Just like in the above examples, we have used the function on a string, we can also use the function on the array of strings.

Example 3: With array of string

In the example below we will apply the split() method on an array of strings.

import numpy as np

inp = np.array(['Dance-Tonight', 'Sleep-Tonight', 'Walk-Tonight','Study-Tonight']) 
print ("The original Input array : \n", inp) 

output= np.char.split(inp, sep='-', maxsplit=1)
print ("The output split array: ", output)

The original Input array :
['Dance-Tonight' 'Sleep-Tonight' 'Walk-Tonight' 'Study-Tonight']
The output split array: [list(['Dance', 'Tonight']) list(['Sleep', 'Tonight'])
list(['Walk', 'Tonight']) list(['Study', 'Tonight'])]


In this tutorial we have covered the split() function of the Numpy Library with multiple code examples to help you understand the split() function.