Open In App

Repeat all the Elements of a NumPy Array of Strings

Last Updated : 27 Sep, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

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.

Python
import numpy as np
arr = np.array(['One', 'Two', 'Three'])
res = np.char.multiply(arr, 2)
print(res)

Output
['OneOne' 'TwoTwo' 'ThreeThree']

Syntax

numpy.char.multiply(a, i)

Parameters:

  • a: Input array of strings.
  • i: Number of times each string is repeated.

Return Value: Returns a new array where each string is repeated i times.

Examples

Example 1: In this example, we repeat each string in the array 3 times.

Python
import numpy as np
arr = np.array(['Luca', 'Sofia', 'Hiroshi', 'Elena', 'Mateo'])
res = np.char.multiply(arr, 3)
print(res)

Output
['LucaLucaLuca' 'SofiaSofiaSofia' 'HiroshiHiroshiHiroshi'
 'ElenaElenaElena' 'MateoMateoMateo']

Example 2: In this example, we create an array and repeat each element 2 times.

Python
import numpy as np
arr = np.array(['Geeks', 'for', 'Geeks'])
res = np.char.multiply(arr, 2)
print(res)

Output
['GeeksGeeks' 'forfor' 'GeeksGeeks']

Example 3: In this example, instead of np.char.multiply(), we use a Python list comprehension to repeat the strings.

Python
import numpy as np
arr = np.array(['Python', 'is', 'fun'])
n = 2
res = np.array([s * n for s in arr])
print(res)

Output
['PythonPython' 'isis' 'funfun']

Explanation: Each string is multiplied manually using s * n, then stored in a NumPy array. 


Explore