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:
Python - NumPy Code Editor:
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/string-operations/ljust.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics