Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

numpy.defchararray.center() in Python

  • Last Updated : 28 Nov, 2018

numpy.core.defchararray.center(arr, width, fillchar): Centers the element of the string element-wise.

Parameters:
arr : array-like or string.
width : Length og resulting string.
fillchar : Padding character. Default is space.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Returns : Copy of array with centerd string.



Code #1:




# Python Program illustrating 
# numpy.char.center() method 
import numpy as np 
  
arr1 = ['eAAAa', 'ttttds', 'AAtAAt']
print ("arr1 : ", arr1)
  
print ("\narr1 : ", np.char.center(arr1, 7, fillchar ='z'))
print ("\narr1 : ", np.char.center(arr1, [9, 9, 11],
                         fillchar =['z', '1', '3']))
  
print ("\narr1 : ", np.char.center(arr1, 11, fillchar ='z'))

Output:

arr1 :  ['eAAAa', 'ttttds', 'AAtAAt']

 centered arr1 :  ['zeAAAaz' 'zttttds' 'zAAtAAt']

 centered arr1 :  ['zzeAAAazz' '11ttttds1' '333AAtAAt33']

 centered arr1 :  ['zzzeAAAazzz' 'zzzttttdszz' 'zzzAAtAAtzz']

 
Code #2:




# Python Program illustrating 
# numpy.char.center() method 
import numpy as np 
  
arr2 = ['11sf', 'sdsf2', '1111f2']
print ("arr2 : ", arr2)
  
# Will throw Error
print ("\narr2 : ", np.char.center(arr2))

Output:

arr2 :  ['11sf', 'sdsf2', '1111f2']

TypeError: center() missing 1 required positional argument: 'width'



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!