numpy.char.ljust#
- char.ljust(a, width, fillchar=' ')[源代码]#
返回一个数组,其中 a 的元素在长度为 width 的字符串中左对齐。
- 参数:
- a类数组,具有
StringDType、bytes_或str_数据类型 - width类数组,具有任何整数 dtype
结果字符串的长度,除非
width < str_len(a)。- fillchar类数组,具有
StringDType、bytes_或str_dtype 用于填充的可选字符(默认为空格)。
- a类数组,具有
- 返回:
- outndarray
输出
StringDType、bytes_或str_数据类型的数组,取决于输入类型
另请参阅
备注
虽然
a和fillchar可以具有不同的 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')