![]() |
VOOZH | about |
In NumPy, you can easily repeat each string in an array multiple times. The function numpy.char.multiply() allows you to repeat every element in a string array by a given number. This is useful when performing text preprocessing, string manipulation or data augmentation.
Example: Here’s a simple example that repeats each string 2 times.
['OneOne' 'TwoTwo' 'ThreeThree']
numpy.char.multiply(a, i)
Parameters:
Return Value: Returns a new array where each string is repeated i times.
Example 1: In this example, we repeat each string in the array 3 times.
['LucaLucaLuca' 'SofiaSofiaSofia' 'HiroshiHiroshiHiroshi' 'ElenaElenaElena' 'MateoMateoMateo']
Example 2: In this example, we create an array and repeat each element 2 times.
['GeeksGeeks' 'forfor' 'GeeksGeeks']
Example 3: In this example, instead of np.char.multiply(), we use a Python list comprehension to repeat the strings.
['PythonPython' 'isis' 'funfun']
Explanation: Each string is multiplied manually using s * n, then stored in a NumPy array.