numpy.char.ljust#

char.ljust(a, width, fillchar=' ')[源代码]#

返回一个数组,其中 a 的元素在长度为 width 的字符串中左对齐。

参数:
aarray-like, 具有 StringDType, bytes_, 或 str_ dtype
widtharray_like, 具有任意整数 dtype

结果字符串的长度,除非 width < str_len(a)

fillchararray-like, 具有 StringDType, bytes_, 或 str_ dtype

可选的填充字符(默认为空格)。

返回:
outndarray

根据输入类型,输出数组为 StringDType, bytes_str_ dtype

另请参阅

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')