Q:

Write a NumPy program to add two zeros to the beginning of each element of a given array of string values

0

Write a NumPy program to add two zeros to the beginning of each element of a given array of string values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np 

nums = np.array(['1.12', '2.23', '3.71', '4.23', '5.11'], dtype=np.str)
print("Original array:")
print(nums)
print("\nAdd two zeros to the beginning of each element of the said array:")
print(np.char.add('00', nums))
print("\nAlternate method:")
print(np.char.rjust(nums, 6, fillchar='0'))

Sample Output:

Original array:
['1.12' '2.23' '3.71' '4.23' '5.11']

Add two zeros to the beginning of each element of the said array:
['001.12' '002.23' '003.71' '004.23' '005.11']

Alternate method:
['001.12' '002.23' '003.71' '004.23' '005.11']

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now