NumPy Join Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the join() function of the NumPy library in Python. The join() function adds a separator character or string to any given string or array of strings. It is similar to the str.join() function in Python.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing the required Libraries

To use the join() function, we need to import the NumPy library in our Python code. We can import it using the following code:

import numpy as np

Using join() with a simple string

In this step, we will use the join() function on a simple string to understand its basic functionality. The following code demonstrates the usage:

a = np.char.join(':','DG')
print("The Joined string in the output:")
print(a)

The output of the above code will be:

The Joined string in the output:
D:G

Using join() with an array of strings

In this step, we will use the join() function on an array of strings. We will use different separator characters for each string element of the array. The following code demonstrates the usage:

inp = np.array(['Apple', 'Python', 'NumPy','StudyTonight'])
print ("The original Input array : \n", inp)

sep = np.array(['^', '+', '*','-'])

output= np.char.join(sep, inp)
print ("The Output joined array: ", output)

The output of the above code will be:

The original Input array :
['Apple' 'Python' 'NumPy' 'StudyTonight']
The Output joined array: ['A^p^p^l^e' 'P+y+t+h+o+n' 'N*u*m*P*y' 'S-t-u-d-y-T-o-n-i-g-h-t']

Using a single separator string with join()

In this step, we will use a single separator string for all the string elements of the given array. The following code demonstrates the usage:

inp = np.array(['Apple', 'Python', 'NumPy','StudyTonight'])
print ("The original Input array : \n", inp)

sep = np.array(['^^^'])

output= np.char.join(sep, inp)
print ("The Output joined array: ", output)

The output of the above code will be:

The original Input array :
['Apple' 'Python' 'NumPy' 'StudyTonight']
The Output joined array: ['A^^^p^^^p^^^l^^^e' 'P^^^y^^^t^^^h^^^o^^^n' 'N^^^u^^^m^^^P^^^y'
'S^^^t^^^u^^^d^^^y^^^T^^^o^^^n^^^i^^^g^^^h^^^t']

Summary

In this lab, we learned about the join() function of the NumPy Library, which is used to add a separator to any string or to all the elements of a string array by providing a separator character or string.

Conclusion

The join() function is a useful function for concatenating strings by adding a separator between them. We can use it to join an array of strings or single string with a separator of our choice.

Summary

Congratulations! You have completed the NumPy Join Function lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like