w3resource

NumPy String: numpy.char.join() function

numpy.char.join()function

The numpy.char.join() function is used to concatenate elements of a string array using a specified delimiter.

This function is useful in -

  • Data preprocessing: When working with data containing string arrays, you may need to concatenate elements using a specific delimiter for further analysis or manipulation. The numpy.char.join() function can be used to perform this task efficiently.
  • Data import and export: When importing or exporting data between different systems or file formats, you may need to concatenate string arrays using a specific delimiter to ensure compatibility and consistency across systems.
  • Data cleaning: In some cases, you may need to correct inconsistencies within a dataset by joining string arrays using a specified delimiter.

Syntax:

numpy.char.join(sep, seq)

Parameters:

Name Description Required /
Optional
sep array_like of str or unicode Required
seq array_like of str or unicode Required

Return value:

Returns a new array with the same shape as the input array, where each element is a string resulting from joining the input elements using the specified delimiter.

Example: Joining characters of a string with a delimiter using NumPy

>>> import numpy as np
>>> x = np.char.join(':', 'ABC')
>>> x
array('A:B:C', dtype='<U5')

In the above code the numpy.char.join() function is called with the delimiter ':' and the input string 'ABC'. This function concatenates the characters of the input string using the specified delimiter and returns a 0-dimensional array (scalar) containing the resulting joined string.

Pictorial Presentation:

NumPy String operation: join() function

Example: Joining characters of multiple strings with different delimiters using NumPy

>>> import numpy as np
>>> x = np.char.join([':','*'],['ABC', 'XYZ'])
>>> x
array(['A:B:C', 'X*Y*Z'], dtype='<U5')

In the above example numpy.char.join() function is called with two input lists: the first list contains the delimiters ([':', '*']), and the second list contains the input strings (['ABC', 'XYZ']). This function concatenates the characters of the input strings using their corresponding specified delimiters and returns a one-dimensional array containing the resulting joined strings.

Pictorial Presentation:

NumPy String operation: join() function

Python - NumPy Code Editor:

Previous: encode()
Next: ljust()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/string-operations/join.php