﻿ NumPy: Add two zeros to the beginning of each element of a given array of string values - w3resource # NumPy: Add two zeros to the beginning of each element of a given array of string values

## NumPy String: Exercise-19 with Solution

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

Sample Solution:

Python Code:

``````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("\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']
```

Python-Numpy Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Debug With the Print() Function:

```>>> for i in range(5):
...     print(i, end=', ' if i < 4 else '\n')
...
0, 1, 2, 3, 4
>>> for i in range(5):
...     print(f'{i} & {i*i}', end=', ' if i < 4 else '\n')
...
0 & 0, 1 & 1, 2 & 4, 3 & 9, 4 & 16
```