w3resource
NumPy String Exercises

NumPy: Repeat all the elements three times of a given array of string

NumPy String: Exercise-2 with Solution

Write a NumPy program to repeat all the elements three times of a given array of string.

Sample Solution:-

Python Code:

import numpy as np
x1 = np.array(['Python', 'PHP', 'Java', 'C++'], dtype=np.str)
print("Original Array:")
print(x1)
new_array = np.char.multiply(x1, 3)
print("New array:")
print(new_array)

Sample Input:

(['Python', 'PHP', 'Java', 'C++'], dtype=np.str)

Sample Output:

Original Array:
['Python' 'PHP' 'Java' 'C++']
New array:
['PythonPythonPython' 'PHPPHPPHP' 'JavaJavaJava' 'C++C++C++']

Pictorial Presentation:

NumPy String: Repeat all the elements three times of a given array of string

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to concatenate element-wise two arrays of string.
Next: Write a NumPy program to capitalize the first letter, lowercase, uppercase, swapcase, title-case of all the elements of a given array.

What is the difficulty level of this exercise?