NumPy provides function np.char.join(), which joins characters of a string using a specified separator.
import numpy as np
arr = np.array(["Hello"])
res = np.char.join(" ", arr)
print(res)
Output
['H e l l o']
Syntax
np.char.join(sep, arr)
Parameters:
- sep: separator to insert between characters.
- arr: Input array of strings.
Return Value: Returns a new array where each string has its characters joined by the separator.
Examples
Example 1: In this example, we insert spaces between the characters of each string in the array.
import numpy as np
arr = np.array(["Geeks", "For", "Geeks"])
res = np.char.join(" ", arr)
print(res)
Output
['G e e k s' 'F o r' 'G e e k s']
Example 2: This example shows how we can use a different separator to join characters.
import numpy as np
arr = np.array(["Data", "Science"])
res = np.char.join("-", arr)
print(res)
Output
['D-a-t-a' 'S-c-i-e-n-c-e']
Example 3: Here, we format numeric strings by adding spaces between digits.
import numpy as np
arr = np.array(["1234", "5678"])
res = np.char.join(" ", arr)
print(res)
Output
['1 2 3 4' '5 6 7 8']