NumPy strings.ljust()
The numpy.strings.ljust()
function left-justifies each string element in an array within a specified width.
If the string is shorter than the specified width, it is padded with spaces or a specified fill character.
Syntax
numpy.strings.ljust(a, width, fillchar=' ')
Parameters
Parameter | Type | Description |
---|---|---|
a | array-like (StringDType, bytes_, or str_) | Array of strings to be left-justified. |
width | array_like (integer dtype) | The total length of each resulting string after justification. |
fillchar | array-like (StringDType, bytes_, or str_), optional | The character used for padding (default is space). |
Return Value
Returns an ndarray of strings, bytes, or StringDType, depending on the input type. Each string is left-justified to the specified width.
Examples
1. Left-Justifying Strings with Spaces
In this example, we left-justify an array of fruit names to a width of 10 characters using the default space character.
import numpy as np
# Define an array of fruit names
fruits = np.array(["apple", "banana", "cherry"], dtype="U10")
# Left-justify with a width of 10 using spaces (default)
result = np.strings.ljust(fruits, 10)
# Print the results
print("Left-justified strings with spaces:")
print(result)
Output:
Left-justified strings with spaces:
['apple ' 'banana ' 'cherry ']

Each string is padded with spaces on the right to make its length equal to 10.
2. Left-Justifying Strings with a Custom Fill Character
Here, we use the '-'
character instead of spaces for padding.
import numpy as np
# Define an array of fruit names
fruits = np.array(["apple", "banana", "cherry"], dtype="U10")
# Left-justify with a width of 10 using a custom fill character '-'
result = np.strings.ljust(fruits, 10, fillchar='-')
# Print the results
print("Left-justified strings with '-' as fill character:")
print(result)
Output:
['apple-----' 'banana----' 'cherry----']

Each string is now padded with '-'
instead of spaces.
3. Using Different Widths for Each String
We specify different widths for each string to justify them dynamically.
import numpy as np
# Define an array of fruit names
fruits = np.array(["apple", "banana", "cherry"], dtype="U10")
# Define different widths for each string
widths = np.array([8, 12, 15])
# Left-justify strings with different widths
result = np.strings.ljust(fruits, widths)
# Print the results
print("Left-justified strings with varying widths:")
print(result)
Output:
Left-justified strings with varying widths:
['apple ' 'banana ' 'cherry ']

Each string is left-justified according to its corresponding width value.
4. Left-Justifying Byte Strings
We justify byte strings instead of Unicode strings.
import numpy as np
# Define an array of fruit names as byte strings
fruits = np.array([b"apple", b"banana", b"cherry"], dtype="S10")
# Left-justify with a width of 10 using spaces
result = np.strings.ljust(fruits, 10)
# Print the results
print("Left-justified byte strings with spaces:")
print(result)
Output:
Left-justified byte strings with spaces:
[b'apple ' b'banana ' b'cherry ']

Byte strings are also left-justified using the specified width.