目录
1. 对齐填充字符
填充字符只能1个字符长的Unicode字符,fillchar为空则实际填充空格字符:' '
x='A'
#填充字符超长
y=x.center(5,'12')
Traceback (most recent call last):
File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-27-bcff94cd4426>", line 1, in <module>
y=x.center(5,'12')
TypeError: The fill character must be exactly one character long
#填充字符非unicode字符
y = x.center(10,1)
Traceback (most recent call last):
File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-29-69914bbffbc3>", line 1, in <module>
y = x.center(10,1)
TypeError: The fill character must be a unicode character, not int
2. center(width,fillchar='')
返回一个字符居中的新字符串(width <= 字符串长度,新字符串 = 原字符串;width > 字符串宽度,所有字符居中,左右使用 fillchar 参数指定的字符填充)
#width <= 字符串长度,新字符串 = 原字符串
x = '012345'
y = x.center(4,'0')
y
'012345'
y = x.center(6,'0')
y
'012345'
#width > 字符串宽度,所有字符左对齐,右侧使用 fillchar 参数指定的字符填充
y = x.center(10,