3. Write a Python Numpy program to print the string in the center, left-justified and right-justified with length 5 and 10.
The output is:
Original Array: ['python', 'numpy', 'pandas']
Expected Answer:
Center_5_width = ['pytho' 'numpy' 'panda']
Left_5_width = ['pytho' 'numpy' 'panda']
Right_5_width = ['pytho' 'numpy' 'panda']
Center_10_width = ['__python__' '__numpy___' '__pandas__']
Left_10_width = ['python____' 'numpy_____' 'pandas____']
Right_10_width = ['____python' '_____numpy' '____pandas']
Sample Answer:
import numpy as np
x = np.array(['python', 'numpy', 'pandas'], dtype=np.str)
print("Original Array:", x)
center_5 = np.char.center(x, 5, fillchar='_')
left_5 = np.char.ljust(x, 5, fillchar='_')
right_5 = np.char.rjust(x, 5, fillchar='_')
center_10 = np.char.center(x, 10, fillchar='_')
left_10 = np.char.ljust(x, 10, fillchar='_')
right_10 = np.char.rjust(x, 10, fillchar='_')
print("\nCenter_5_width =", center_5)
print("Left_5_width =", left_5)
print("Right_5_width =", right_5)
print("\nCenter_10_width =", center_10)
print("Left_10_width =", left_10)
print("Right_10_width =", right_10)
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)