What is the numpy.ljust() function from NumPy in Python?

Overview

The char.ljust() function in Python is used to return an array with the length of an input array that is left-justified in a string of a given length.

Syntax

char.ljust(a, width, fillchar=' ')

Parameter value

The char.ljust() function takes the following parameter values:

  • a: This represents the input array.
  • width: This represents the length of the output string.
  • fillchar: This represents the character that is used for padding.

Return value

The char.ljust() function returns an array of strings or Unicode. The value that is returned depends on the input type in the function.

Code example

import numpy as np
# ll
myarray = np.char.ljust('Theo', 10, fillchar = '*')
print(myarray)

Code explanation

  • Line 4: We create a string character called "Theo". Then, we choose to return an array of string with a length of 10 and for the remaining spaces after the "Theo" to be filled with the character "*".

Free Resources