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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/lists -.-> lab-86470{{"`NumPy Join Function`"}} python/tuples -.-> lab-86470{{"`NumPy Join Function`"}} python/importing_modules -.-> lab-86470{{"`NumPy Join Function`"}} python/standard_libraries -.-> lab-86470{{"`NumPy Join Function`"}} python/numerical_computing -.-> lab-86470{{"`NumPy Join Function`"}} python/build_in_functions -.-> lab-86470{{"`NumPy Join Function`"}} numpy/1d_array -.-> lab-86470{{"`NumPy Join Function`"}} numpy/data_array -.-> lab-86470{{"`NumPy Join Function`"}} numpy/bool_idx -.-> lab-86470{{"`NumPy Join Function`"}} numpy/fancy_idx -.-> lab-86470{{"`NumPy Join Function`"}} end

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