w3resource

NumPy String: numpy.char.rjust() function

numpy.char.rjust() function

The numpy.char.rjust() function returns an array with the elements of a right-justified in a string of length width.

The function can be helpful when we want to align text or format strings in tabular data or when generating console output that requires a consistent column layout.

Syntax:

numpy.char.rjust(a, width, fillchar=' ')

Parameters:

Name Description Required /
Optional
a array-like of str or unicode Required
width: int The length of the resulting strings Require
fillchar: str or unicode The character to use for padding. Optional

Return value:

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

Example: Right-Justifying a String using NumPy char.rjust()

>>> import numpy as np
>>> x = np.char.rjust('w3resource', 30, fillchar = '#')
>>> x
array('####################w3resource', dtype='<U30')

The above example shows how to use numpy.char.rjust() to right-justify a given string within a specified width by padding it with a specified fill character. The result is an array containing the right-justified string '####################w3resource' with a dtype of '<U30'.

Pictorial Presentation:

NumPy String operation: rjust() function

Python - NumPy Code Editor:

Previous: replace()
Next: rpartition()



Follow us on Facebook and Twitter for latest update.