Python numpy.char.multiply() Method (With Examples)

By IncludeHelp Last updated : November 29, 2023

Python numpy.char.multiply() Method

The multiply() method is a built-in method of char class in the numpy module, it is used to perform element-wise multiple concatenation.

Module

The following module is required to use the multiply() method:

import numpy as np

Syntax

The following is the syntax of the multiply() method:

char.multiply(arr, i)

Parameter(s)

The following are the parameter(s):

  • arr: The array-like value which can be a string, array of strings, or Unicode.
  • i: An int type value or an array of ints, number of times to be repeated.

Return Value

The return type of the multiply() method is <class 'numpy.ndarray'>, it returns element-wise multiple concatenation.

Example 1: Using array of strings and a single int value

# Importing numpy module import numpy as np # Creating two arrays with # arrays of strings arr1 = ["Hello", "Hi", "Hey", "Include"] arr2 = ["World", "Earth", "Sun", "Help"] # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2) # Using char.add() method result = np.char.add(arr1, arr2) print("\nThe concatenated array is (result):\n", result) print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['Hello', 'Hi', 'Hey']
i:  4

The result is:
 ['HelloHelloHelloHello' 'HiHiHiHi' 'HeyHeyHeyHey']

The type of the result is: <class 'numpy.ndarray'>

Example 2: Using array of strings and array of integers

# Importing numpy module import numpy as np # Creating an array of strings arr1 = ["Hello", "Hi", "Hey", "Include"] # Creating an array of ints i = [1, 2, 3, 4] # Printing the arrays print("arr1: ", arr1) print("i: ", i) # Using char.multiply() method result = np.char.multiply(arr1, i) print("\nThe result is:\n", result) print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['Hello', 'Hi', 'Hey', 'Include']
i:  [1, 2, 3, 4]

The result is:
 ['Hello' 'HiHi' 'HeyHeyHey' 'IncludeIncludeIncludeInclude']

The type of the result is: <class 'numpy.ndarray'>

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.