operations with empty strings | ljust () function

numpy.core.defchararray.ljust (arr, width, fillchar = & # 39; & # 39;) — this is another function for doing string operations in numpy. Returns an array with left-aligned arr elements in a length-width string. It fills the remaining space of each element in the array with the fillchr parameter fillchr . If fillchr is not passed, the remaining spaces are filled with blank space.

Parameters:
arr: array_like of str or unicode.Input array.
width: The final width of the each string.
fillchar: The character to fill in remaining space.

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

Code # 1:

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

 
# numpy import

import numpy as geek

 
# input array 

in_arr = geek.array ([ `Numpy` , ` Python` , `Pandas` ])

print ( "Input array:" , in_arr) 

 
# set the width of each line to 8

width = 8

 
# output array when fillchar is not supplied

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

print ( " Output left justified array: " , out_arr) 

Output :

 Input array: [`Numpy`` Python` `Pandas`] Output left justified array: [`Numpy` `Python` `Pandas`] 

Code # 2:

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

 
# numpy import

import numpy as geek

 
# input array

in_arr = geek.array ([ `Numpy` `Python` , ` Pandas` ])

print ( " Input array: " , in_arr) 

  
# set the width of each line to 8

width = 8

 
# output array

out_arr = geek.char.ljust (in_arr, width, fillchar = `*` )

print ( "Output left justified array:" , out_ar r) 

Exit:

 Input array: [`Numpy`` Python` `Pandas`] Output left justified array: [` Numpy *** `` Python ** `` Pandas ** `] 

Code # 3:

# Python program explaining
# numpy. char.ljust () 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.ljust (in_arr, width, fillchar = `-` )

print ( "Output left justified array : " , out_arr) 

Output:

 Input array: [`1`` 11` `111`] Ou tput left justified array: [`1 ----` `11 ---` `111 -`]