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(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']
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to check whether each element of a given array starts with "P".
Next: Write a NumPy program to replace a specific character with another in a given array of string values.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Returns the symmetric difference between two lists, after applying the provided function to each list element of both:
Example:
def tips_symmetric_difference_by(p, q, fn): _p, _q = set(map(fn, p)), set(map(fn, q)) return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p] from math import floor print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))
Output:
[2.4, 6.8]
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework