What is the numpy.char.join() function from NumPy in Python?

Overview

The char.join() function in Python’s NumPy library is used to concatenate an array of strings in a given sequence seq. The char.join() function returns a string in which each of the array elements is joined by a specified separator character.

Syntax

char.join(sep, seq)

Function parameters

The char.join() function takes the following parameter values:

  • sep: This represents the array of strings or unicodes that will be used as the separator characters.
  • seq: This represents the input array that needs to be concatenated.

Return value

The char.join() function returns an array of strings or unicodes.

Code example

import numpy as np
# creating an array string to be concatenated
myarray = np.array(['Apple', 'Python', 'NumPy','Educative'])
print ("The Input array : \n", myarray)
# creating the seperator characters
separray = np.array(['__'])
print ("\n")
newarray= np.char.join(separray, myarray)
print ("The concatenated array: \n", newarray)

Code explanation

  • Line 1: We import the numpy module.
  • Line 4: We create the input array, myarray, using the array() function.
  • Line 5: We print the array called myarray.
  • Line 8: We create the separator array, separray, using the array() function.
  • Line 12: We implement the char.join() function on both the arrays. Then, we assign the result to another variable called newarray.
  • Line 14: We print the variable newarray to the console.

Free Resources