Friday, July 24, 2026
HomeLanguagesnumpy string operations | zfill() function

numpy string operations | zfill() function

numpy.core.defchararray.zfill(arr, width) is another function for doing string operations in numpy. For each element in the array it returns the numeric string left-filled with zeros.The number of left filled zeros happen according to the width.

Parameters:
arr : array_like of str or unicode.Input array.
width : [int] The final width of the string after filling zeros.

Returns : [ndarray] Output array of str or unicode, depending on input type.

Code #1 :




# Python program explaining
# numpy.char.zfill() method 
  
# importing numpy 
import numpy as geek
  
# input array  
in_arr = geek.array(['Geeks', 'for', 'Geeks'])
print ("Input array : ", in_arr) 
  
# setting the width of each string to 8
width = 8
  
# output array
out_arr = geek.char.zfill(in_arr, width)
print ("Output array: ", out_arr) 


Output:

Input array :  ['Geeks' 'for' 'Geeks']
Output array:  ['000Geeks' '00000for' '000Geeks']

 
Code #2 :




# Python program explaining
# numpy.char.zfill() method 
  
# importing numpy 
import numpy as geek
  
# input array  
in_arr = geek.array(['1', '11', '111'])
print ("Input array : ", in_arr)
  
# setting the width of each string to 5
width = 5
  
# output array
out_arr = geek.char.zfill(in_arr, width)
print ("Output array: ", out_arr) 


Output:

Input array :  ['1' '11' '111']
Output array:  ['00001' '00011' '00111']
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS