w3resource

NumPy String: numpy.char.ljust() function

numpy.char.ljust()function

The numpy.char.ljust() function is used to left-justify elements of a string array by adding padding characters to the right side of each element.

This function is useful in data formatting:, text alignment etc.

Syntax:

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

Parameters:

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

Return value:

Array of str or unicode, depending on input type

Example: Left-Justifying a string with padding characters using NumPy

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

In the above example the numpy.char.ljust() function is called with the input string 'w3resource', a width parameter of 30, and a fillchar parameter set to the character '#'. This function left-justifies the input string by adding '#' characters to the right side until the total width of the resulting string is 30 characters.

Pictorial Presentation:

NumPy String operation: ljust() function

Python - NumPy Code Editor:

Previous: join()
Next: lower()



Follow us on Facebook and Twitter for latest update.