NumPy strings.multiply()
The numpy.strings.multiply() function performs element-wise string repetition. It multiplies each string element by an integer, effectively concatenating the string that many times.
Syntax
</>
Copy
numpy.strings.multiply(a, i)
Parameters
| Parameter | Type | Description |
|---|---|---|
a | array_like (StringDType, bytes_ or str_ dtype) | Input array containing strings to be multiplied. |
i | array_like (integer dtype) | Array of integers specifying how many times each string should be repeated. |
Return Value
Returns an array where each string element in a is repeated according to the corresponding integer in i. If i is less than 0, the result is an empty string.
Examples
1. Repeating a Single String
We repeat a single string multiple times.
</>
Copy
import numpy as np
# Define a string
fruit = np.array("apple", dtype="U")
# Repeat the string 3 times
result = np.strings.multiply(fruit, 3)
# Print the result
print("Repeated string:", result)
Output:
Repeated string: appleappleapple
