NumPy char | rjust method
np.char.rjust(~) method adds padding to the input strings so that they are of the specified length, and places the strings at the right side.
The input array.
The desired length of each string.
The characters to fill with if the specified width exceeds the size of the input string. By default, a empty space will be added.
A NumPy array of strings, with each string being exactly of size
width and extra spaces filled with
Cases when size of string is larger than width
np.char.rjust(["abcd", "efg"], 2)array(['ab', 'ef'], dtype='<U2')
Since the specified width is larger than the size of the strings, the first 2 characters were extracted. You may be wondering why the last two characters were selected (after all it's right-justify) - cases like this when the input string overflows, it's always the first two characters that will be extracted. The right behavior applies for other cases.
Case when padding is required
np.char.rjust(["abcd", "e"], 2)array(['ab', ' e'], dtype='<U2')
Notice how an empty space was added to
' e' to ensure each returned string is of length 2, and how the character was right-aligned.
Specifying a custom filler
Instead of an empty whitespace, we can specify our own characters to pad with:
np.char.rjust(["abcd", "e"], 2, "z")array(['ab', 'ze'], dtype='<U2')
Here, instead of
" e", we get