operations with empty strings | zfill () function



numpy.core.defchararray.zfill (arr, width) — this is another function for doing string operations in numpy. For each element in the array, it returns a numeric string filled with zeros on the left. The number of padded zeros on the left occurs 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:

# Program Python explaining
# numpy.char.zfill () method

 
# numpy import

import numpy as geek

 
# input array

in_arr  = geek.array ([ `Geeks` , `for` , ` Geeks` ])

print ( "Input array: " , in_arr) 

  
# set the width of each line to 8

width = 8

 
# output array

out_arr = geek.char.zfill (in_arr, width )

print ( "Output array: " , out_arr) 

 

Exit:

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

Code # 2:

# Python program explaining
# numpy.char.zfill () method

  
# numpy import

import numpy as geek

 
# input array

in_arr = geek.array ([ `1` , ` 11` , `111` ])

  print ( "Input array:" , in_arr)

 
# set the width of each line to 5

width = 5

 
# output array

out_arr = geek.char.zfill (in_arr, width)

print ( "Output array:" , out_arr) 

Exit:

 Input array: [`1`` 11` `111`] Output array: [` 00001` `00011`` 00111`]