Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: multiply() function

numpy.core.defchararray.multiply() function

Return (a * i), that is string multiple concatenation, element-wise.
Values in i of less than 0 are treated as 0 (which yields an empty string).

Version: 1.15.0

numpy.core.defchararray.multiply

Syntax:

numpy.core.defchararray.multiply(a, i)

Parameter:

Name Description Required /
Optional
a: array_like of str or unicode Input array. Required
i: array_like of ints Input array Required

Return value:

out : ndarray - Output array of str or unicode, depending on input types

Note:

The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example-1: numpy.multiply() function

import numpy as np  
a1 = ['aaa', 'bbb', 'ccc'] 
a2 = ['ppp', 'qqq', 'rrr'] 
print("\na1 : ", a1) 
print("\na2 : ", a2) 
print("\na1 : ", np.char.multiply(a1, 2)) 
print ("\na1 : ", np.char.multiply(a1, [2, 4, 3])) 
print ("\na2 : ", np.char.multiply(a2, 3)) 

Output:

a1 :  ['aaa', 'bbb', 'ccc']

a2 :  ['ppp', 'qqq', 'rrr']

a1 :  ['aaaaaa' 'bbbbbb' 'cccccc']

a1 :  ['aaaaaa' 'bbbbbbbbbbbb' 'ccccccccc']

a2 :  ['ppppppppp' 'qqqqqqqqq' 'rrrrrrrrr']

Pictorial Presentation:

NumPy String operation: multiply() function

Pictorial Presentation:

NumPy String operation: multiply() function

Pictorial Presentation:

NumPy String operation: multiply() function

Example-2: numpy.multiply() function

import numpy as np  
arr1 = 'Python'
arr2 = 'NumPy Tutorial '
print ("\narr1 : ", np.char.multiply(arr1, 2)) 
print ("\narr2 : ", np.char.multiply(arr2, 4)) 

Output:

arr1 :  PythonPython

arr2 :  NumPy Tutorial NumPy Tutorial NumPy Tutorial NumPy Tutorial 

Python - NumPy Code Editor:

Previous: add()
Next: mod()