Skip to content
Related Articles

Related Articles

How to insert a space between characters of all the elements of a given NumPy array?
  • Last Updated : 29 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

In this article, we will discuss how to insert a space between the characters of all elements of a given array of string. 

Suppose we have an array of string as follows:
A = ["geeks", "for", "geeks"]

then when we insert a space between the characters
of all elements of the above array we get the 
following output.
A = ["g e e k s", "f o r", "g e e k s"]

To do this we will use np.char.join(). This method basically returns a string in which the individual characters are joined by separator character that is specified in the method. Here the separator character used is space. 

Syntax: np.char.join(sep, string) 

sep: is any specified separator 
string: is any specified string. 



# importing numpy as np
import numpy as np
# creating array of string
x = np.array(["geeks", "for", "geeks"],
print("Printing the Original Array:")
# inserting space using np.char.join()
r = np.char.join(" ", x)
print("Printing the array after inserting space\
between the elements")


Printing the Original Array:
['geeks' 'for' 'geeks']
Printing the array after inserting spacebetween the elements
['g e e k s' 'f o r' 'g e e k s']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :