6. Write a Python Numpy program to remove the whitespaces of the left side, whitespace of right side and all whitespaces between the initial array.
The output is:
Initial Array = [' python ', ' numpy ', ' pandas ', ' free ', ' exercises ']
Expected Answer:
Initial Array: [' python ' ' numpy ' ' pandas ' ' free ' ' exercises ']
Remove the left side whitespaces : ['python ' 'numpy ' 'pandas ' 'free ' 'exercises ']
Remove the right side whitespaces : [' python' ' numpy' ' pandas' ' free' ' exercises']
Remove the leading and trailing whitespaces: ['python' 'numpy' 'pandas' 'free' 'exercises']
Sample Answer:
import numpy as np a = np.array([' python ', ' numpy ', ' pandas ', ' free ', ' exercises '], dtype=np.str) print("Initial Array:", a) lremove_space = np.char.lstrip(a) print("\nRemove the left side whitespaces : ", lremove_space) rremove_space = np.char.rstrip(a) print("\nRemove the right side whitespaces : ", rremove_space) remove_space = np.char.strip(a) print("\nRemove the leading and trailing whitespaces: ", remove_space)
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)