Open In App

How to split the element of a given NumPy array with spaces?

Last Updated : 27 Sep, 2025
Suggest changes
Share
Like Article
Like
Report

In NumPy, string operations can be performed directly on arrays of strings using functions from the numpy.char module. One useful function is numpy.char.split(), which splits each element of a string array into a list of words based on a given separator (default is whitespace).

In the below example, we split a single string in a NumPy array into words separated by spaces.

Python
import numpy as np
arr = np.array(['Hello World'])
res = np.char.split(arr)
print(res)

Output
[list(['Hello', 'World'])]

Explanation: The string "Hello World" is split into two words: "Hello" and "World".

Syntax

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

Parameters:

  • arr: Input array of strings.
  • sep: Separator to split on (default is space).
  • maxsplit: Maximum number of splits to perform.

Return Value: Returns an array of lists where each string is split into words.

Examples

Example 1: In this example, we split a string containing several programming languages.

Python
import numpy as np
arr = np.array(['PHP C# Python C Java C++'])
res = np.char.split(arr)
print(res)

Output
[list(['PHP', 'C#', 'Python', 'C', 'Java', 'C++'])]

Example 2: In this example, we split a sentence "Geeks For Geeks" into individual words.

Python
import numpy as np
arr = np.array(['Geeks For Geeks'])
res = np.char.split(arr)
print(res)

Output
[list(['Geeks', 'For', 'Geeks'])]

Example 3: Here, we split a string containing subject names separated by spaces.

Python
import numpy as np
arr = np.array(['DBMS OOPS DS'])
res = np.char.split(arr)
print(res)

Output
[list(['DBMS', 'OOPS', 'DS'])]

Explore