What is the numpy.rjust() function from NumPy in Python?
Overview
The char.rjust() function in Python is used to return an array with the length of an input array that is right-justified in a string of a given length.
Syntax
char.rjust(a, width, fillchar=' ')
Parameter
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.rjust() function returns an array of strings or Unicode. The returned value depends on the input type in the function.
Example
Let’s look at the code below:
import numpy as np# implementing the char.rjust() functionmyarray = np.char.rjust('Theo', 10, fillchar = '*')print(myarray)
Explanation
- Line 4: We create a string character called
"Theo". Then, we choose to return an array of string with a length of10, which is to be filled with the character"*"at a particular number of times just before"Theo"is added. It makes the resulting array a total length of10characters.