numpy.char.center#
- char.center(a, width, fillchar=' ')[source]#
傳回 a 的副本,其元素在長度為 width 的字串中置中。
- 參數:
- aarray-like,具有
StringDType
、bytes_
或str_
dtype - widtharray-like,具有任何整數 dtype
結果字串的長度,除非
width < str_len(a)
。- fillchararray-like,具有
StringDType
、bytes_
或str_
dtype 要使用的選用填充字元(預設為空格)。
- aarray-like,具有
- 傳回值:
- outndarray
輸出
StringDType
、bytes_
或str_
dtype 的陣列,取決於輸入類型
參見
註解
雖然
a
和fillchar
可能具有不同的 dtype,但在a
的 dtype 為 “S” 時,在fillchar
中傳遞非 ASCII 字元是不允許的,並且會引發ValueError
。範例
>>> import numpy as np >>> c = np.array(['a1b2','1b2a','b2a1','2a1b']); c array(['a1b2', '1b2a', 'b2a1', '2a1b'], dtype='<U4') >>> np.strings.center(c, width=9) array([' a1b2 ', ' 1b2a ', ' b2a1 ', ' 2a1b '], dtype='<U9') >>> np.strings.center(c, width=9, fillchar='*') array(['***a1b2**', '***1b2a**', '***b2a1**', '***2a1b**'], dtype='<U9') >>> np.strings.center(c, width=1) array(['a1b2', '1b2a', 'b2a1', '2a1b'], dtype='<U4')