numpy.strings.ljust#

strings.ljust(a, width, fillchar=' ')[source]#

返回一個陣列,其中的元素 a 在長度為 width 的字串中靠左對齊。

參數:
a類陣列 (array-like),具有 StringDTypebytes_str_ dtype
width類陣列 (array-like),具有任何整數 dtype

結果字串的長度,除非 width < str_len(a)

fillchar類陣列 (array-like),具有 StringDTypebytes_str_ dtype

用於填充的可選字元(預設為空格)。

返回:
outndarray

輸出陣列,其 dtype 為 StringDTypebytes_str_,取決於輸入類型

參見

str.ljust

註解

雖然 afillchar 可以具有不同的 dtype,但當 a 的 dtype 為 “S” 時,不允許在 fillchar 中傳遞非 ASCII 字元,並且會引發 ValueError

範例

>>> import numpy as np
>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> np.strings.ljust(c, width=3)
array(['aAaAaA', '  aA  ', 'abBABba'], dtype='<U7')
>>> np.strings.ljust(c, width=9)
array(['aAaAaA   ', '  aA     ', 'abBABba  '], dtype='<U9')