Tuesday, October 7, 2025
HomeLanguagesnumpy.defchararray.center() in Python

numpy.defchararray.center() in Python

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

Parameters: 
arr : array-like or string. 
width : Length of resulting string. 
fillchar : Padding character. Default is space.
Returns : Copy of array with centered string.

Code #1: 
 

Python3




# 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: 
 

Python3




# 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'

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS